home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / FST30A.ZIP;1 / FMODULA2.DOC < prev    next >
Encoding:
Text File  |  1992-09-27  |  126.4 KB  |  4,190 lines

  1.  
  2.  
  3.                         FST (Fitted) Modula-2 Version 3.0
  4.  
  5.                (C) Copyright 1987,1988,1992 Fitted Software Tools.
  6.                               All rights reserved.
  7.  
  8.  
  9.                               Fitted Software Tools
  10.                                  P.O.Box 867403
  11.                                  Plano, TX 75086
  12.                                 BBS 214/517-4629
  13.  
  14.  
  15.  
  16.  
  17.      
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.                              DISCLAIMER OF WARRANTY
  29.  
  30.      THIS SOFTWARE AND MANUAL ARE PROVIDED "AS IS" AND WITHOUT WARRANTIES
  31.      AS TO PERFORMANCE OR MERCHANTABILITY.
  32.  
  33.      THIS SOFTWARE IS PROVIDED WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES
  34.      WHATSOEVER. BECAUSE OF THE DIVERSITY OF CONDITIONS AND HARDWARE UNDER
  35.      WHICH THIS SOFTWARE MAY BE USED, NO WARRANTY OF FITNESS FOR A
  36.      PARTICULAR PURPOSE IS OFFERED. THE USER IS ADVISED TO TEST THIS
  37.      SOFTWARE THOROUGHLY BEFORE RELYING ON IT. THE USER MUST ASSUME THE
  38.      ENTIRE RISK OF USING THIS SOFTWARE.
  39.  
  40.      
  41.  
  42.      All the information in this document is believed to be correct at the
  43.      time of publication.  We do, however, reserve the right to make any
  44.      changes in product specifications and/or availability without notice. 
  45.  
  46.      
  47.  
  48.  
  49.  
  50.  
  51.  
  52.      IBM is a registered trademark of International Business Machines
  53.      Corporation.
  54.  
  55.      1
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.                                   To my parents
  67.  
  68.  
  69.  
  70.  
  71.  
  72.      I implemented FST Modula-2 for my mother; I could not have done it
  73.      without my father's support and encouragement. 
  74.  
  75.      Everything that is good in me, I owe them. 
  76.  
  77.      
  78.  
  79.                                                    Roger Carvalho
  80.  
  81.      
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.      Introduction                                                         3
  120.      
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.                                     Chapter 1
  129.  
  130.                                   Introduction
  131.  
  132.  
  133.  
  134.      Thank you for your interest in our Modula-2 compiler. 
  135.  
  136.      This system features a Modula-2 compiler with an integrated editor and
  137.      "make" facility, a program linker, a makefile generator, and an
  138.      execution profiler. 
  139.  
  140.      The compiler generates code for the Intel 8086 "huge" or "large"
  141.      memory model: In the "huge" memory model, each module has its own data
  142.      and code segment, each of which can be up to 64k in size; In the
  143.      "large" memory model, all the modules' static data is combined into a
  144.      single data segment.  In either model, pointers are 4 bytes long and
  145.      all the leftover memory is available for the "heap". More restrictive
  146.      memory models are not supported. 
  147.  
  148.      All the library and runtime support source code is available to our
  149.      registered users (see Shareware).
  150.  
  151.      We hope that our effort will prove itself worthy of your support. 
  152.  
  153.      
  154.  
  155.  
  156.  
  157.  
  158.      1.1 A few words from the author
  159.  
  160.  
  161.      I dedicated many years' worth of my spare time to the development of
  162.      this product. 
  163.  
  164.      FST Modula-2 v1.0 was my third attempt at a complete Modula-2
  165.      compiler.  From the first two, I reused the knowledge acquired. 
  166.  
  167.      Why Modula-2? Remember the Pascal vs C debate?  Well, I liked (and
  168.      used) both.  I saw Modula-2 as providing the best of both, plus some
  169.      more. 
  170.  
  171.      A safe language that let you do all the low level stuff?  Utopia? No,
  172.      this was for real! 
  173.  
  174.      Ten years after I first met Modula-2, I still do most of my
  175.  
  176.  
  177.  
  178.      Introduction                                                         4
  179.      
  180.  
  181.      programming in C...
  182.  
  183.      I am often asked if I will implement the forthcoming ISO standard.  I
  184.      will not track the development of the standard; When the standard is
  185.      published (my understanding is that this will not happen for at least
  186.      another year), I will decide what to do. 
  187.  
  188.      Here is what I am planning for the future -- please keep in mind that
  189.      I am not promising anything and that my drive to do this will be
  190.      somewhat proportional to the interest generated by this release of the
  191.      compiler. 
  192.  
  193.      Many users have asked for a debugger.  Since I do not much care for
  194.      debuggers, I have been resisting.  Well, no more.  I started work on a
  195.      debugger.  I still don't know when the debugger will be ready; I can,
  196.      however, tell you what it is: A simple debugger with only the basics
  197.      (step, run to breakpoint, examine variables, ... ); But you will get
  198.      the full source code and can hack it to your heart's content.  How
  199.      does that sound?! 
  200.  
  201.      At the same time that I introduce the debugger, I will discontinue
  202.      support for OBJ files.  In retrospect, adding OBJ file support was a
  203.      bad decision.  If I add support for Windows, M2Link will be changed to
  204.      handle DLLs.
  205.  
  206.      I will continue to work on the object oriented extensions too, if the
  207.      ones that I am introducing are well accepted. 
  208.  
  209.      So, if you care about any of this, one way or another, please let me
  210.      know! 
  211.  
  212.      
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.      Introduction                                                         5
  238.      
  239.  
  240.  
  241.  
  242.  
  243.  
  244.      1.2 Hardware requirements
  245.  
  246.  
  247.      This system will run on IBM PC, PC/AT, or compatible systems with at
  248.      least 512K of RAM, two double sided floppy disk drives and a
  249.      monochrome display adapter, color graphics adapter or equivalent. 
  250.  
  251.      A hard disk and 640K of RAM are, however, recommended. 
  252.  
  253.      
  254.  
  255.  
  256.  
  257.  
  258.      1.3 Software requirements
  259.  
  260.  
  261.      This system requires DOS version 2.0 or later. 
  262.  
  263.      No other software is required to use this system, but you will need an
  264.      assembler if you intend to modify one of the following runtime support
  265.      modules: M2Reals (floating point support), M2Longs (LONG arithmetic)
  266.      or M2Procs (coroutine handling). 
  267.  
  268.      
  269.  
  270.  
  271.  
  272.  
  273.      1.4 For users upgrading to release 3
  274.  
  275.  
  276.      We are happy to announce that, finally, the system is smart enough to
  277.      report runtime errors by module name and line number! 
  278.  
  279.      Release 3.0 also adds support for object oriented programming.  Please
  280.      refer to the chapter on Classes for more information. 
  281.  
  282.      
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.      Software installation                                                6
  297.      
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.                                     Chapter 2
  306.  
  307.                               Software installation
  308.  
  309.  
  310.  
  311.      Before starting, please print the file READ.ME, which provides
  312.      information about the distibution contents. 
  313.  
  314.      
  315.  
  316.  
  317.  
  318.  
  319.      2.1 Theory
  320.  
  321.  
  322.      The library module TermIO (through which Terminal does its I/O)
  323.      requires that the ANSI.SYS driver be installed. 
  324.  
  325.      The executable files (MC.EXE, M2LINK.EXE, ...) should, for ease of
  326.      operation, be placed in a drive/directory named in the DOS PATH
  327.      variable. 
  328.  
  329.      The editor configuration file M2ED.CFG must be accessible through the
  330.      DOS PATH.
  331.  
  332.      Both the compiler and the linker will use the environment variable
  333.      M2LIB to locate required library modules (the format for the M2LIB
  334.      entry matches that of the DOS PATH).
  335.  
  336.      The environment variable M2MODEL may be set to 'HUGE' or 'LARGE'. IF
  337.      M2MODEL is not set, the compiler, by default, assumes that you want to
  338.      compile for the HUGE model. 
  339.  
  340.      The compiler and utilities in this package do not keep many files open
  341.      simultaneously.  If you set the FILES parameter in CONFIG.SYS to 10
  342.      you should not encounter any problems.  For performance
  343.      considerations, you should also allocate more than the default number
  344.      of buffers for DOS: If you are running on a PC class machine, try
  345.      allocating 20 buffers in your CONFIG.SYS. On an AT class machine, try
  346.      60 to 99 buffers. 
  347.  
  348.      Example CONFIG.SYS:
  349.  
  350.           DEVICE=ANSI.SYS
  351.           FILES=10
  352.  
  353.  
  354.  
  355.      Software installation                                                7
  356.      
  357.  
  358.           BUFFERS=20
  359.  
  360.      This compiler is a memory hog -- you heard it here first!  This is
  361.      neither good nor bad, simply an implementation decision.  Source
  362.      modules being compiled, for example, are sucked into memory with a
  363.      single read system call. 
  364.  
  365.      Source files (either being compiled or edited) are loaded at the top
  366.      of available memory.  This, of course clobbers the resident portion of
  367.      COMMAND.COM. Because this software was developed on a relatively slow
  368.      system, with a very slow hard disk, we decided that it would be a good
  369.      idea to, on request, preserve the resident portion of COMMAND.COM.
  370.      Unless you are running a floppy based system, do not worry about
  371.      this. 
  372.  
  373.      To enable the "COMMAND save" feature of the compiler, which will stop
  374.      it from clobbering the resident portion of COMMAND.COM, just add the
  375.      following line to your AUTOEXEC.BAT file:
  376.  
  377.           SET CMDSIZE=NN
  378.  
  379.      where NN is the size of the resident portion of COMMAND.COM in K (18
  380.      for DOS 3.0).
  381.  
  382.      MC clobbers the top of the memory space available for use during
  383.      initialization.  Therefore, to find out the size of the resident
  384.      portion of COMMAND.COM, try the following procedure: start by setting
  385.      CMDSIZE to the size of COMMAND.COM. If now you execute MC, exit it,
  386.      and press the F3 key, DOS will still remember your MC command. 
  387.      Decrement CMDSIZE and retry the MC test until the system forgets your
  388.      last command (COMMAND.COM had to be reloaded). 
  389.  
  390.      The compiler supports 2 different memory models.  If you are going to
  391.      be using both memory models, keep each set of library object files in
  392.      a different directory, and change the M2LIB path according to the
  393.      model in use.  We use the following BAT files to take care of this:
  394.  
  395.  
  396.      LARGE.BAT:
  397.           set M2MODEL=LARGE
  398.           set M2LIB=C:\M2\LIB;C:\M2\LIB\L
  399.  
  400.      HUGE.BAT:
  401.           set M2MODEL=HUGE
  402.           set M2LIB=C:\M2\LIB;C:\M2\LIB\H
  403.  
  404.      where \M2\LIB is the directory where all the DEF and BIN files reside,
  405.      \M2\LIB\L contains the LARGE memory model M2O files,... 
  406.  
  407.      In the following discussion we will, for simplicity sake, assume that
  408.      you will be using only one of the memory models. 
  409.  
  410.      
  411.  
  412.  
  413.  
  414.      Software installation                                                8
  415.      
  416.  
  417.  
  418.  
  419.  
  420.  
  421.      2.2 Recommended setup for a system with 2 floppy drives
  422.  
  423.  
  424.      This whole system is too large to fit in a single 360k floppy. 
  425.      Therefore, it is recommended that you build a "compiler" floppy and a
  426.      "utilities" floppy (you may combine them both in a 720k or larger
  427.      floppy). 
  428.  
  429.      On the compiler floppy, place the compiler (MC.EXE), the file M2ED.CFG
  430.      and DOS' COMMAND.COM.
  431.  
  432.      The utilities floppy will take the rest of the basic system: the
  433.      linker (M2LINK.EXE), and the library and runtime support object files
  434.      (*.M2O and *.BIN) and other .EXE files that you may want and happen to
  435.      fit. 
  436.  
  437.      On your work disk you must create the directory \LIB and copy all the
  438.      library definitions modules (*.DEF) to it. 
  439.  
  440.      Assuming that you will use drive A for the compiler and utilities disk
  441.      and drive B as your work drive, add the following to your AUTOEXEC.BAT
  442.      file:
  443.  
  444.           SET PATH=A:
  445.           SET M2LIB=B:\LIB;A:
  446.  
  447.      If you follow these suggestions, you may use most of the system's
  448.      capabilities.  Just remember to swap the floppy in drive A before and
  449.      after invoking the linker from the compiler's menu. 
  450.  
  451.  
  452.  
  453.  
  454.      2.3 Recommended setup for a hard disk system
  455.  
  456.  
  457.      Place the executable files (*.EXE) and M2ED.CFG in a directory
  458.      currently in the DOS search path, or in a new directory (ex: \M2) to
  459.      be added to the PATH list.  Example:
  460.  
  461.           SET PATH=C:\BIN;C:\M2
  462.  
  463.      Make a directory for the library files (ex: \M2\LIB) and copy all the
  464.      .DEF, .M2O and .BIN files to it.  To the AUTOEXEC.BAT file add the
  465.      line:
  466.  
  467.           SET M2LIB=C:\M2\LIB
  468.  
  469.      You will probably want to create a directory for your own reusable
  470.  
  471.  
  472.  
  473.      Software installation                                                9
  474.      
  475.  
  476.      library modules.  This directory can be added to the M2LIB environment
  477.      variable.  Example:
  478.  
  479.           SET M2LIB=C:\M2\LIB;C:\MYLIB
  480.  
  481.      You may keep your projects in their own, individual, directories. 
  482.  
  483.      
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      A little tour through the system                                    10
  533.      
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.                                     Chapter 3
  542.  
  543.                         A little tour through the system
  544.  
  545.  
  546.  
  547.      After you finish the installation as described above, please copy the
  548.      files identified as the "system tour files" in the READ.ME file to
  549.      your work disk or directory. 
  550.  
  551.      Because we do not know your particular system configuration, in the
  552.      following examples we will assume the worst possible scenario. 
  553.      Therefore, some of the capabilities of this system will not be fully
  554.      exploited.  Specifically, we will not invoke the linker or run the
  555.      programs from the compiler menu. 
  556.  
  557.      
  558.  
  559.  
  560.  
  561.  
  562.      3.1 The tour
  563.  
  564.  
  565.      First, we will look at the unnatural case of compiling, linking and
  566.      running a program that works the first time around.  Please execute
  567.      the following commands:
  568.  
  569.           MC SIEVE /C
  570.           M2LINK SIEVE /O
  571.  
  572.      Well, that's it!  Ready to run... 
  573.  
  574.           SIEVE
  575.  
  576.      Now for the more usual case:
  577.  
  578.           MC BADSIEVE
  579.  
  580.      From the compiler menu, select 'C' for compile. 
  581.  
  582.      Gee, that was quick!  Press RETURN to take a look at our errors... 
  583.  
  584.      The cursor is now positioned at the location of the first error. 
  585.      Using the keys Ctrl-E (find next error) and Ctrl-P (find previous
  586.      error) you may visit all the errors flagged by the compiler.  All the
  587.      while, the editor shows the error description on the top line of the
  588.  
  589.  
  590.  
  591.      A little tour through the system                                    11
  592.      
  593.  
  594.      screen.  But, going back to the first error... 
  595.  
  596.      We really confused the compiler when we mistakenly typed in '.'
  597.      instead of '..' in the range declaration.  Move the cursor back (left
  598.      arrow key) to where the '.' is, type in another '.', and that should
  599.      fix that!  As you will see as you type Ctrl-E, that single error
  600.      caused the compiler to dislike a few other things on that same line;
  601.      we will just ignore those errors and go on. 
  602.  
  603.      Go on to the next error location (line 22). This time, we should have
  604.      used a ']' but typed '}' instead.  The backspace key will delete the
  605.      offending character; now, type the ']' in its place. 
  606.  
  607.      And that is that.  Shall we try to compile the program again? 
  608.  
  609.      Press Alt-S to save the file, Alt-Q to leave the editor and, back at
  610.      the main menu, select 'C'.
  611.  
  612.      More errors?! 
  613.  
  614.           These errors should have been detected during pass2 of the
  615.           compiler; If not, you may try to fix these errors and recompile,
  616.           or you may opt for loading and compiling the new file
  617.           BADSIEV1.MOD, which contains the earlier fixes.
  618.  
  619.      Going back to the editor... 
  620.  
  621.      We find that we used the identifier 'cnt' which is undefined; we
  622.      really meant to use count.  So, moving the cursor around with the
  623.      cursor keys and/or deleting characters with the backspace or delete
  624.      key, please replace 'cnt' by 'count'.
  625.  
  626.      Searching for the next error... 
  627.  
  628.      The next error occurred during the processing of the call to
  629.      WriteCard. What happened here is that WriteCard requires 2 parameters,
  630.      the second one being the size of the field to display.  So, to fix it,
  631.      let's insert a comma after 'count' and some number (for example
  632.      'count,4').
  633.  
  634.      Any more errors?  No, that is it... 
  635.  
  636.      We now save the file (Alt-S), quit the editor (Alt-Q) and recompile
  637.      (c).
  638.  
  639.      Now, the program should have compiled without errors.  If not, you may
  640.      recompile BADSIEV2.MOD instead. 
  641.  
  642.      We may now quit the compiler (q).
  643.  
  644.      By now, we have some program that has compiled clean (BADSIEVE,
  645.      BADSIEV1 or BADSIEV2). In what follows, we will assume all went well
  646.      and we have BADSIEVE.
  647.  
  648.  
  649.  
  650.      A little tour through the system                                    12
  651.      
  652.  
  653.  
  654.      If you look at the directory, you will see the new object module
  655.      created as a result of the previous exercise (BADSIEVE.M2O). The
  656.      compiler always writes its output to a file with the extension 'M2O'.
  657.  
  658.      Let us link and test the program:
  659.  
  660.           M2LINK BADSIEVE /L
  661.  
  662.      We use the /L option so that the line number information written out
  663.      by the compiler to the object file will be preserved by the linker. 
  664.      We will need this information later. 
  665.  
  666.           BADSIEVE
  667.  
  668.      Hmmm... A runtime error at line 22.
  669.  
  670.      Let's see what happened... 
  671.  
  672.           MC BADSIEVE
  673.  
  674.      Pick 'E' to go into the editor and, either move the cursor down to the
  675.      line indicated, or let the editor find it with Alt-G.
  676.  
  677.      So, what is the problem?  Well, we declared the 'flag' array to have a
  678.      maximum index of 8190, but 'j' got bigger than that (the FOR loop will
  679.      increment 'j' up to the value of 10000).
  680.  
  681.      You may fix the problem by deleting the '10000' and typing in its
  682.      place 'SIZE'.
  683.  
  684.      Recompile the program, link it, and run it.  Did it work?  Good.
  685.  
  686.      It is time for you to experiment on your own.  But, before you do much
  687.      more, you may want to check out the Editor chapter of the
  688.      documentation. 
  689.  
  690.      
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.      The Compiler                                                        13
  710.      
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.                                     Chapter 4
  719.  
  720.                                   The Compiler
  721.  
  722.  
  723.  
  724.      In the Modula-2 language, as defined by Niklaus Wirth, identifiers may
  725.      be used before they are declared, except when they are used in another
  726.      declaration (this restriction does not apply to pointers).  This
  727.      forces the compilation process to be done in at least two passes. 
  728.  
  729.      To avoid imposing unnecessary restrictions and, yet, provide
  730.      reasonable performance, the two pass approach was selected: During the
  731.      first pass, syntax analysis and declaration analysis are performed;
  732.      The second pass performs the semantic analysis and code generation. 
  733.  
  734.      The compiler has an integrated text editor.  Should errors be
  735.      encountered, the editor is invoked at the end of the current compiler
  736.      pass (sooner, if an error is found during the processing of an import
  737.      list or if 20 errors are identified). 
  738.  
  739.      The compiler also has a built in "make" processor.  A makefile must be
  740.      created before this process is invoked.  Although you can create a
  741.      makefile using the editor, we recommend that you use the utility
  742.      provided for that purpose: GENMAKE (this utility may be invoked from
  743.      the compiler menu -- G).
  744.  
  745.      The compiler can generate 2 different kinds of object files as
  746.      output.  By default, M2O (stands for "Modula-2 Object") files are
  747.      generated.  This file format is unique to this compiler, and it is
  748.      optimized for our requirements and those of Modula-2. But the user
  749.      can, through the use of an environment variable (M2OUTPUT), specify
  750.      that standard OBJ files are to be generated instead. 
  751.  
  752.      
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.      The Compiler                                                        14
  769.      
  770.  
  771.  
  772.  
  773.  
  774.  
  775.      4.1 Running the integrated compiler: MC
  776.  
  777.  
  778.      Compiler invocation:
  779.  
  780.           MC [workModule] [/p mainModule] [/s maxIds idSpace] [/c] [/e]
  781.           [/m] [/d-]
  782.  
  783.      The '/p' option may be used to indicate the name of the main module of
  784.      the program that you are working on (the name should not have an
  785.      extension).  If this option is not used, but workModule is entered
  786.      without an extension, the same name is also used for mainModule. 
  787.  
  788.      The '/s' option allows you to change the default sizes of the compiler
  789.      identifier tables.  The two arguments specify the maximum number of
  790.      different identifiers to be processed, and the total string space to
  791.      be allocated to store these identifiers.  The default values are 2000
  792.      and 12000, respectively. 
  793.  
  794.      If you use of the '/c' command line option, the compiler starts
  795.      compiling "workModule" immediately and, if no errors are encountered,
  796.      will bring you right back to the DOS prompt.  This is useful when
  797.      running the compiler from a batch file:
  798.  
  799.           MC myprog /c
  800.  
  801.      The '/e' option will send you straight into the editor. 
  802.  
  803.      The '/m' option invokes the make processor, which will look for the
  804.      file mainModule.MAK for the dependencies list. 
  805.  
  806.      The '/d-' option sets the default value of the compiler directives $R
  807.      and $T to '-', disabling the default generation of most runtime error
  808.      checking.  We do not recommend disabling the stack overflow checking,
  809.      and the use of '/d-' will not do it. 
  810.  
  811.      The compiler always sets the DOS errorlevel to 0 if the last compile
  812.      was successful (no errors); otherwise, the DOS errorlevel is set to 1.
  813.  
  814.      If the compiler is invoked without the '/c', '/e' or '/m' options, you
  815.      will get a screen that looks something like this:
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.      The Compiler                                                        15
  828.      
  829.  
  830.  
  831.  
  832.  
  833.           Modula-2 compiler, Version 3.0
  834.           (C) Copyright 1987-1992 Fitted Software Tools. All rights reserved.
  835.  
  836.           Memory model in use: LARGE
  837.           Output file format:  M2O
  838.  
  839.           Heap in use:                   0K
  840.           Available Heap:              294K
  841.  
  842.           Program:
  843.  
  844.           Work module: work.mod
  845.  
  846.           Program  New   DOS      Quit
  847.           Compile  Edit  Genmake  Make  Link  eXecute >
  848.  
  849.  
  850.  
  851.      The options at this point are:
  852.  
  853.      Program           Specify the name of the main program module. 
  854.  
  855.      New               Specify another "Work module". 
  856.  
  857.      DOS               Invoke a new DOS shell.  At the DOS prompt, you
  858.                        should type EXIT to return to this system. 
  859.  
  860.      Quit              Return to DOS.
  861.  
  862.      Compile           Compile the "Work module". 
  863.  
  864.      Edit              Edit the "Work module". 
  865.  
  866.      Genmake           Invoke the GenMake program, passing as argument the
  867.                        name in Program -- and '/OBJ', if appropriate. 
  868.  
  869.      Make              Recompile all the necessary modules as per the rules
  870.                        of a makefile (The makefile is assumed to have the
  871.                        name in Program and the extension of .MAK). Note: If
  872.                        errors are encountered during the compilation of one
  873.                        of the modules, the make process is aborted.  After
  874.                        fixing the errors, select Make again. 
  875.  
  876.      Link              Invokes the linker (M2Link) passing along as
  877.                        arguments the name in Program and '/L'.
  878.  
  879.      eXecute           You are prompted for any arguments that you may want
  880.                        to pass to the program; The Program is then
  881.                        executed. 
  882.  
  883.  
  884.  
  885.  
  886.      The Compiler                                                        16
  887.      
  888.  
  889.      
  890.  
  891.  
  892.  
  893.  
  894.      4.2 Running the freestanding compiler: M2COMP
  895.  
  896.  
  897.      Compiler invocation:
  898.  
  899.           M2COMP filename [/m] [/s maxIds idSpace] [/d-]
  900.  
  901.      filename is the name of the module to compile or, if the /M option is
  902.      used, the name of the makefile to process. 
  903.  
  904.      The DOS errorexit is set to 0 if the compilation (make) is successful
  905.      and to 1 otherwise. 
  906.  
  907.      
  908.  
  909.  
  910.  
  911.  
  912.      4.3 The compilation process
  913.  
  914.  
  915.      
  916.  
  917.  
  918.      4.3.0.1 The input file
  919.  
  920.      If the module to be compiled is already loaded into one of the editor
  921.      buffers, that source is compiled.  Otherwise, the compiler tries to
  922.      open the named file. 
  923.  
  924.      
  925.  
  926.  
  927.      4.3.0.2 The imported modules
  928.  
  929.      The compiler and the linker cooperate in assuring that all the modules
  930.      that refer to a particular definition module will have been compiled
  931.      against the same version of that definition module. 
  932.  
  933.      To this end, the compiler places in the 'module header' and 'module
  934.      import' records of the object file a "module key".  This module key is
  935.      the date of the DEF file used during the compilation of the
  936.      implementation module or during the processing of the IMPORT
  937.      statement. 
  938.  
  939.      Due to this, the compiler will not look in the editor buffers for the
  940.      DEF files needed to process an IMPORT list.  These are always read in
  941.      from the disk. 
  942.  
  943.  
  944.  
  945.      The Compiler                                                        17
  946.      
  947.  
  948.  
  949.      
  950.  
  951.  
  952.      4.3.0.3 The output file
  953.  
  954.      The output from the compilation of a main module or an implementation
  955.      module is a single output file, with the same name of the source file
  956.      but with the extension of 'M2O' (Modula-2 Object) -- OBJ files are
  957.      created instead, if the environment variable M2OUTPUT so specifies. 
  958.  
  959.      The compilation of a definition module does not generate any new
  960.      output files.  If the compilation is successful (no errors), the
  961.      compiler simply 'touches' the source file, updating its modification
  962.      time. 
  963.  
  964.      
  965.  
  966.  
  967.      4.3.0.4 A warning
  968.  
  969.      Because of the fact that the compiler uses the date of the DEF file as
  970.      that module's key, you may not modify a DEF file unless you intend to
  971.      recompile all the modules that use it, nor can you copy the file in
  972.      such a way that its date is not preserved. 
  973.  
  974.      In particular, if you are going to be transferring your modules
  975.      between computers, you must use some procedure that will preserve all
  976.      the DEF files' dates. 
  977.  
  978.      This is probably a good place to point out that, when you use OBJ
  979.      files, you are not protected by this module version checking. 
  980.  
  981.      
  982.  
  983.  
  984.  
  985.  
  986.      4.4 Compiler directives
  987.  
  988.  
  989.      Certain compiler code generation options may be set through directives
  990.      included in the program text.  These directives must appear
  991.      immediately at the beginning of a comment; multiple directives may be
  992.      entered in a single comment by separating them by commas.  Example
  993.  
  994.           (* $S-, $R+ *)
  995.  
  996.      A '+' sets the directive to TRUE, a '-' sets it to FALSE, and a '='
  997.      pops the directive's value prior to the last '+' or '-'.
  998.  
  999.      The following compiler directives are defined:
  1000.  
  1001.  
  1002.  
  1003.  
  1004.      The Compiler                                                        18
  1005.      
  1006.  
  1007.      $A                Alignment. Default $A+. If enabled, all new variables
  1008.                        declared are aligned on a word boundary.  Record
  1009.                        fields are packed (not aligned) regardless of the
  1010.                        setting of this option. 
  1011.  
  1012.      $S                Stack overflow checking.  Default $S+. If enabled,
  1013.                        stack overflow checking is performed on entry to a
  1014.                        procedure and when copying open arrays to a
  1015.                        procedure's local stack frame. 
  1016.  
  1017.      $R                Range checking.  Default $R+. If enabled, before any
  1018.                        assignment is made to a variable of a subrange type,
  1019.                        the value to be assigned is tested against the limits
  1020.                        of the subrange type. 
  1021.  
  1022.      $T                Array subscript and NIL pointer checking.  Default
  1023.                        $T+. If enabled, any time a subscript operation is
  1024.                        performed on an array, the subscript value is checked
  1025.                        to confirm that the operation would not generate an
  1026.                        address outside the bounds of the array.  In
  1027.                        addition, before a pointer is dereferenced, its value
  1028.                        is checked for NIL.
  1029.  
  1030.      $L                Generate line number information.  Default $L-. If
  1031.                        this option is enabled, the compiler will include a
  1032.                        list of source code line numbers and their
  1033.                        corresponding object code offsets in the output
  1034.                        file.  This line number information is passed on to
  1035.                        the .DBG file when the program is linked with the /L
  1036.                        option. 
  1037.  
  1038.      
  1039.  
  1040.  
  1041.      4.4.0.1 Benchmarks
  1042.  
  1043.      If you are going to benchmark the code generated by this compiler,
  1044.      please disable the generation of runtime error checking on all the
  1045.      compilers involved in the comparison. 
  1046.  
  1047.      We feel compelled to spell this out because some other vendors'
  1048.      products do not generate runtime error checking by default. 
  1049.  
  1050.      
  1051.  
  1052.  
  1053.  
  1054.  
  1055.      4.5 Runtime errors
  1056.  
  1057.  
  1058.      When, during the execution of a program, a runtime error is detected,
  1059.      the runtime error handler will terminate the program and write out a
  1060.  
  1061.  
  1062.  
  1063.      The Compiler                                                        19
  1064.      
  1065.  
  1066.      message indicating the type of error encountered and its location
  1067.      (module name, line number and PC address). 
  1068.  
  1069.      
  1070.  
  1071.  
  1072.      4.5.1 Trapping runtime errors in your program
  1073.  
  1074.      The Library module System provides you with a means of intercepting
  1075.      runtime errors.  The following are the currently defined runtime error
  1076.      numbers that may be passed to your error handler routine:
  1077.  
  1078.           0   stack overflow ($S option)
  1079.           1   range error ($R or $T option)
  1080.           2   integer/cardinal overflow (divide by zero)
  1081.           3   floating point error
  1082.           4   function did not execute a RETURN
  1083.           5   HALT was invoked
  1084.  
  1085.  
  1086.  
  1087.  
  1088.      4.6 Compiler size limits
  1089.  
  1090.  
  1091.      The following are the code and data size limits imposed by this
  1092.      compiler:
  1093.  
  1094.        -  A string constant cannot exceed 80 characters.  This is also the
  1095.           limit set for the size of any identifier. 
  1096.  
  1097.        -  When using the HUGE memory model, each compilation module is
  1098.           assigned its own data segment, which can be up to 64k in size. 
  1099.           In the data segment, the compiler allocates the space for all the
  1100.           module's global variables and some of the module's constants. 
  1101.  
  1102.        -  When using the LARGE memory model, all the modules' data are
  1103.           combined, at link time, into a single data segment (64k
  1104.           maximum). 
  1105.  
  1106.        -  The maximum size of a data structure is 65532 bytes. 
  1107.  
  1108.        -  The maximum amount of space allocated for variables local to a
  1109.           procedure is 32000 bytes. 
  1110.  
  1111.        -  The compiler will also refuse to generate the code to pass, in a
  1112.           procedure call, by value, a parameter greater than 65000 bytes in
  1113.           size. 
  1114.  
  1115.      The following are the compiler's internal limits:
  1116.  
  1117.        -  The maximum number of different (namewise) identifiers that can
  1118.           be processed in a single compilation is 2000. May be overwritten
  1119.  
  1120.  
  1121.  
  1122.      The Compiler                                                        20
  1123.      
  1124.  
  1125.           at compiler invocation. 
  1126.  
  1127.        -  The total number of characters in all the different (namewise)
  1128.           identifiers processed cannot exceed 12000 characters.  May be
  1129.           overwritten at compiler invocation. 
  1130.  
  1131.        -  No single procedure can be translated into more than 10k of
  1132.           object code. 
  1133.  
  1134.        -  An array of 8k bytes is used to keep track of all the initialized
  1135.           data for a module.  This imposes a limit on the total amount of
  1136.           string, real and long constants used in the compilation module. 
  1137.  
  1138.  
  1139.  
  1140.  
  1141.      4.7 The language supported
  1142.  
  1143.  
  1144.      This release of the compiler will translate a program written in the
  1145.      Modula-2 language as defined by Niklaus Wirth in the 3rd edition of
  1146.      his book "Programming in Modula-2", with the exceptions noted bellow:
  1147.  
  1148.      
  1149.  
  1150.        -  Integer and Cardinal arithmetic overflow is not detected. 
  1151.  
  1152.        -  ASM, CLASS, INHERITED, INIT and DESTROY are reserved word in this
  1153.           implementation. 
  1154.  
  1155.        -  For those programmers that "grew up" in the Hex world, a way to
  1156.           define CHAR literals in Hex is provided: 20X corresponds to the
  1157.           "space" character in ASCII.
  1158.  
  1159.      
  1160.  
  1161.  
  1162.      4.7.1 LONGINT and LONGCARD
  1163.  
  1164.      This compiler implements the standard types LONGINT and LONGCARD.
  1165.  
  1166.      Operands of the type LONGINT or LONGCARD may appear in any expression,
  1167.      just like INTEGER or CARDINAL. But that is about it! 
  1168.  
  1169.      Subranges of these types are not supported. 
  1170.  
  1171.      No standard procedure, except INC, DEC and the ones listed later in
  1172.      this document will accept operands of one of these types. 
  1173.  
  1174.      A variable of type LONGINT or LONGCARD cannot be used as the control
  1175.      variable in a FOR loop.  Neither can CASE labels be of a LONG type. 
  1176.  
  1177.      Constants of type LONGINT or LONGCARD can be coded in decimal only and
  1178.  
  1179.  
  1180.  
  1181.      The Compiler                                                        21
  1182.      
  1183.  
  1184.      must be terminated by an 'L' if the value is less than 65536. Example
  1185.  
  1186.           123L and 123567 are valid LONGCARD or LONGINT constant
  1187.  
  1188.           -1L and -348762 are valid LONGINT constants
  1189.  
  1190.  
  1191.      4.7.2 LONGREAL
  1192.  
  1193.      The standard type LONGREAL is implemented. 
  1194.  
  1195.      The rules for the use of LONGREALs are the same as for REALs.
  1196.  
  1197.      The types REAL and LONGREAL are not compatible, and no automatic
  1198.      conversion from one type to another is ever performed -- the standard
  1199.      procedures SHORT and LONG should be used to convert between these
  1200.      types. 
  1201.  
  1202.      Constants of type LONGREAL are no different from REAL constants.  The
  1203.      type of the constant is determined by context.  You may, however,
  1204.      "type" a constant by the use of the SHORT or LONG procedure.  Ex:
  1205.  
  1206.           CONST longreal1 = LONG(1.0);
  1207.  
  1208.  
  1209.      4.7.3 Additional or augmented standard procedures
  1210.  
  1211.  
  1212.      4.7.3.1 NEW and DISPOSE -- pointer argument.
  1213.  
  1214.      NEW and DISPOSE have been deleted from the language definition in the
  1215.      3rd edition of Wirth's book.  We implement them thus:
  1216.  
  1217.           NEW(p)
  1218.  
  1219.      Invokes the procedure ALLOCATE, which must conform to the type:
  1220.  
  1221.           PROCEDURE ( VAR ADDRESS, CARDINAL )
  1222.  
  1223.      passing along p and the size of the object p is defined as pointing
  1224.      to. 
  1225.  
  1226.           DISPOSE(p)
  1227.  
  1228.      Invokes the procedure DEALLOCATE, which must conform to the type:
  1229.  
  1230.           PROCEDURE ( VAR ADDRESS, CARDINAL )
  1231.  
  1232.      passing along p and the size of the object p is defined as pointing
  1233.      to. 
  1234.  
  1235.      The procedures ALLOCATE and DISPOSE must, therefore, be defined in the
  1236.      module using NEW and/or DISPOSE, or imported from some other module,
  1237.  
  1238.  
  1239.  
  1240.      The Compiler                                                        22
  1241.      
  1242.  
  1243.      like Storage.
  1244.  
  1245.      
  1246.  
  1247.  
  1248.      4.7.3.2 LONG and SHORT
  1249.  
  1250.           PROCEDURE LONG( INTEGER ) :LONGINT;
  1251.  
  1252.           PROCEDURE LONG( CARDINAL ) :LONGCARD;
  1253.  
  1254.           PROCEDURE LONG( REAL ) :LONGREAL;
  1255.  
  1256.           PROCEDURE SHORT( LONGINT ) :INTEGER;
  1257.  
  1258.           PROCEDURE SHORT( LONGCARD ) :CARDINAL;
  1259.  
  1260.           PROCEDURE SHORT( LONGREAL ) :REAL;
  1261.  
  1262.      LONG takes an INTEGER, CARDINAL or REAL and converts it into a LONGINT
  1263.      LONGCARD or LONGREAL, respectively. 
  1264.  
  1265.      SHORT takes a LONGINT, LONGCARD or LONGREAL and converts it into an
  1266.      INTEGER, CARDINAL or REAL, respectively. 
  1267.  
  1268.      
  1269.  
  1270.  
  1271.      4.7.3.3 FLOAT and TRUNC
  1272.  
  1273.      With our two integer/cardinal and real sizes, here is the behavior of
  1274.      the TRUNC and FLOAT procedures. 
  1275.  
  1276.           PROCEDURE FLOAT( CARDINAL ) :REAL;
  1277.  
  1278.           PROCEDURE FLOAT( LONGCARD ) :LONGREAL;
  1279.  
  1280.           PROCEDURE TRUNC( REAL ) :CARDINAL;
  1281.  
  1282.           PROCEDURE TRUNC( LONGREAL ) :LONGCARD;
  1283.  
  1284.      
  1285.  
  1286.  
  1287.  
  1288.  
  1289.      4.8 Objects exported by the pseudo module SYSTEM
  1290.  
  1291.  
  1292.      4.8.0.1 TYPE BYTE
  1293.  
  1294.      Takes 1 byte of storage.  Only assignment is defined for this type. 
  1295.      If the formal parameter of a procedure is of type BYTE, the
  1296.  
  1297.  
  1298.  
  1299.      The Compiler                                                        23
  1300.      
  1301.  
  1302.      corresponding actual parameter may be of any type that takes 1 byte of
  1303.      storage. 
  1304.  
  1305.      If the formal parameter of a procedure is of type ARRAY OF BYTE, the
  1306.      corresponding actual parameter may be of any type. 
  1307.  
  1308.  
  1309.      4.8.0.2 TYPE WORD
  1310.  
  1311.      Takes 1 word (2 bytes) of storage.  Only assignment is defined for
  1312.      this type.  If the formal parameter of a procedure is of type WORD,
  1313.      the corresponding actual parameter may be of any type that takes 1
  1314.      word of storage. 
  1315.  
  1316.      If the formal parameter of a procedure is of type ARRAY OF WORD, the
  1317.      corresponding actual parameter may be of any type.  Care should be
  1318.      taken in this case, as the size of the parameter passed is rounded up
  1319.      to an even size. 
  1320.  
  1321.  
  1322.      4.8.0.3 TYPE ADDRESS
  1323.  
  1324.      The type ADDRESS is compatible with all pointer types.  ADDRESS itself
  1325.      is defined as a POINTER TO WORD. In this implementation, the type
  1326.      ADDRESS is not compatible with any arithmetic type.  This is due to
  1327.      the fact that the Intel 8086 series processors use segmented
  1328.      addresses.  It would not be hard to implement automatic conversions
  1329.      between LONGCARD and ADDRESS but it is felt that this would be
  1330.      contrary to the spirit of the language, whereby the compiler is not
  1331.      expected to perform any "magic" tricks.  Instead, two functions are
  1332.      provided for that purpose: FLAT and PTR.
  1333.  
  1334.      In release 1.2 we relaxed the above a little.  ADDRESS + CARDINAL and
  1335.      ADDRESS - CARDINAL are legal expressions.  The CARDINAL is added or
  1336.      subtracted from the offset portion of the ADDRESS and the result is
  1337.      still an ADDRESS.
  1338.  
  1339.      Also, INC and DEC can take an ADDRESS as their first argument.  The
  1340.      operation is, however, performed on the offset portion of the ADDRESS
  1341.      only. 
  1342.  
  1343.      
  1344.  
  1345.  
  1346.      4.8.0.4 SEG and OFS
  1347.  
  1348.      These are field definitions for POINTER types.  If you import these,
  1349.      you may access the segment or offset portions of a pointer variable
  1350.      using regular field selection syntax.  Example
  1351.  
  1352.           pointer.SEG :segment portion of pointer
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.      The Compiler                                                        24
  1359.      
  1360.  
  1361.      4.8.0.5 PROCEDURE ADR
  1362.  
  1363.      ADR( designator ) Returns the address of designator (type ADDRESS).
  1364.  
  1365.  
  1366.      4.8.0.6 PROCEDURE FLAT
  1367.  
  1368.      FLAT( ADDRESS ) returns a LONGCARD "flat" address. 
  1369.  
  1370.  
  1371.      4.8.0.7 PROCEDURE PTR
  1372.  
  1373.      PTR( LONGCARD ) returns an ADDRESS corresponding to the "flat" address
  1374.      represented by the LONGCARD.
  1375.  
  1376.  
  1377.      4.8.0.8 PROCEDURE SEGMENT
  1378.  
  1379.      SEGMENT( designator ) returns the segment portion of the address of
  1380.      'designator'. Example:
  1381.  
  1382.           DX := SEGMENT( buffer ); would assign to DX the segment value of
  1383.           ADR(buffer).
  1384.  
  1385.  
  1386.      4.8.0.9 PROCEDURE OFFSET
  1387.  
  1388.      OFFSET( designator ) returns the offset portion of the address of
  1389.      'designator'.
  1390.  
  1391.  
  1392.      4.8.0.10 PROCEDURE NEWPROCESS
  1393.  
  1394.      NEWPROCESS(p:PROC; a:ADDRESS; n:CARDINAL; VAR p1:ADDRESS)
  1395.  
  1396.      creates a new process whose entry point is p and workspace is at a for
  1397.      n bytes.  p1 is the new process pointer.  This process is not
  1398.      activated until a TRANSFER to p1 is done. 
  1399.  
  1400.      The starting priority of the new process is the current processor
  1401.      priority at the time NEWPROCESS is invoked (please refer to the
  1402.      section on Module Priorities).
  1403.  
  1404.  
  1405.      4.8.0.11 PROCEDURE TRANSFER
  1406.  
  1407.      TRANSFER( VAR p1, p2 :ADDRESS)
  1408.  
  1409.      suspends the current process, assigning it to p1 and resumes p2.  The
  1410.      current process' value is assigned to p1 only after p2 has been
  1411.      identified; it is, therefore, okay for p1 and p2 to be the same. 
  1412.  
  1413.      The process is resumed at the same priority level that it was running
  1414.  
  1415.  
  1416.  
  1417.      The Compiler                                                        25
  1418.      
  1419.  
  1420.      at, at the time of suspension. 
  1421.  
  1422.  
  1423.      4.8.0.12 PROCEDURE IOTRANSFER
  1424.  
  1425.      IOTRANSFER( VAR p1, p2 :ADDRESS; intVector :CARDINAL )
  1426.  
  1427.      issues a TRANSFER from p1 to p2 (just the way TRANSFER does it) after
  1428.      installing the current process for reactivation when an interrupt
  1429.      comes in through interrupt vector intVector. 
  1430.  
  1431.      When the interrupt occurs, the interrupt vector is reloaded with its
  1432.      previous value.  A TRANSFER is done to the I/O process (the one that
  1433.      issued the IOTRANSFER) such that p2 now contains the value of the
  1434.      process that was running when the interrupt occurred. 
  1435.  
  1436.  
  1437.      4.8.0.13 ASSEMBLER
  1438.  
  1439.      An 8086 inline assembler is provided.  Once ASSEMBLER is imported from
  1440.      SYSTEM, you can enter inline assembler code by bracketing it with the
  1441.      keywords ASM and END.
  1442.  
  1443.      Assembler input is free form.  Comments are entered as in regular
  1444.      Modula-2. Example
  1445.  
  1446.           loop:   CMP  BYTE [SI], 0   (*end of string?*)
  1447.                   MOV  BYTE [DI], [SI]
  1448.                   INC  SI  INC DI     (*increment pointers*)
  1449.                   JMP  loop
  1450.  
  1451.      The assembler accepts all the 8086/8088 opcode mnemonics.  Address
  1452.      operands can be coded in just about any form acceptable to other
  1453.      assemblers, except that the only operator supported if '+'. Operand
  1454.      type overrides are: WORD, BYTE, FAR, NEAR and are not to be followed
  1455.      by the keyword POINTER or PTR. Example
  1456.  
  1457.           label:  MOV  AX, ES:[BX,DI+5]
  1458.                   MOV  AX, ES:5[DI+BX]
  1459.                   MOV  WORD [5], 1
  1460.                   CALL NEAR [DI]
  1461.                   TEST BYTE i+2, 1
  1462.  
  1463.      All the mnemonics and register names must be entered in upper case. 
  1464.      In case you need to use a Modula-2 name that conflicts with one of the
  1465.      assembler reserved symbols, you may precede it with a '@'. Example
  1466.  
  1467.           MOV @AX, AX
  1468.  
  1469.      would generate a move from register AX to variable AX.
  1470.  
  1471.      All modula-2 variables can generally be accessed in assembler.  Record
  1472.      field names are not accessible from assembler.  The assembler will not
  1473.  
  1474.  
  1475.  
  1476.      The Compiler                                                        26
  1477.      
  1478.  
  1479.      automatically do anything for you.  For example: if you specify a VAR
  1480.      parameter as an operand to an instruction, you are naming the address
  1481.      of the pointer to the actual parameter.  Example
  1482.  
  1483.           PROCEDURE p( VAR done :BOOLEAN );
  1484.           ...
  1485.           ASM
  1486.                LES  DI, done
  1487.                MOV  BYTE ES:[DI], TRUE
  1488.           END;
  1489.  
  1490.      is the correct way of storing TRUE in done. 
  1491.  
  1492.      The following types of constants may be accessed in assembler:
  1493.      INTEGER, CARDINAL, BOOLEAN, CHAR and enumeration constants. 
  1494.  
  1495.      All labels declared inside an ASM section are local to that section of
  1496.      code.  But labels names cannot match some name known in the scope of
  1497.      the current procedure.  Labels can only be referenced in jump
  1498.      instructions. 
  1499.  
  1500.      All jumps are optimized by the compiler.  There is, therefore, no need
  1501.      (or capability) to specify the size of a jump.  In particular, the
  1502.      compiler will turn a conditional jump out of range into a reverse
  1503.      conditional jump over a far jump to the original destination. 
  1504.  
  1505.      Remember, this is a Modula-2 compiler, not an assembler!  The inline
  1506.      assembler capability is provided for use in exceptional situations
  1507.      only. 
  1508.  
  1509.      
  1510.  
  1511.  
  1512.      4.8.0.14 ASSEMBLER - 8087 support
  1513.  
  1514.      All the 8087 math coprocessor instructions are supported by the inline
  1515.      assembler.  There are some restrictions, however. 
  1516.  
  1517.      Only the following operand types are supported by the load and store
  1518.      instructions: INTEGER, LONGINT, REAL and LONGREAL. You may not,
  1519.      therefore, load or store a value in temporary real or decimal format. 
  1520.  
  1521.      The meaning of the "no operand" form of the arithmetic instructions
  1522.      was retained:
  1523.  
  1524.           FADD, FSUB, FMUL and FDIV all operate on the two top elements of
  1525.           the 8087 stack, using ST(1) as the destination and removing ST.
  1526.  
  1527.           FSUBR subtracts ST(1) from ST (FDIVR divides ST by ST(1)),
  1528.           leaving the result in ST(1) and removing ST.
  1529.  
  1530.      The 2 operand format of the arithmetic instructions was not
  1531.      implemented.  You may not, therefore, specify a destination register
  1532.  
  1533.  
  1534.  
  1535.      The Compiler                                                        27
  1536.      
  1537.  
  1538.      other than ST, except in the "and pop" versions of the instructions. 
  1539.  
  1540.      With a regular assembler, in register to register operations, you can
  1541.      specify the register that gets the result of the operation (the
  1542.      destination register).  By definition, the destination register is
  1543.      also the first operand of the instruction. 
  1544.  
  1545.      With our inline assembler, ST is always the destination of the
  1546.      operation, except in the "and pop" form of the instructions, in which
  1547.      case the register specified in the instruction "gets the result". 
  1548.  
  1549.      For consistency, we decided that ST should always be the first operand
  1550.      of the instruction, even when the "and pop" form is used. 
  1551.  
  1552.      The meaning of FSUBP, FSUBRP, FDIVP and FDIVRP is, therefore:
  1553.  
  1554.           FSUBP ST(1) means FSUBRP ST(1),ST -> ST(1):=ST-ST(1)
  1555.  
  1556.           FSUBRP ST(1) means FSUBP ST(1),ST -> ST(1):=ST(1)-ST
  1557.  
  1558.           FDIVP ST(1) means FDIVRP ST(1),ST -> ST(1):=ST/ST(1)
  1559.  
  1560.           FDIVRP ST(1) means FDIVP ST(1),ST -> ST(1):=ST(1)/ST
  1561.  
  1562.      and ST is popped. 
  1563.  
  1564.      
  1565.  
  1566.  
  1567.  
  1568.  
  1569.      4.9 The generated object code
  1570.  
  1571.  
  1572.      
  1573.  
  1574.  
  1575.      4.9.1 Data type representation
  1576.  
  1577.      CHAR              1 byte
  1578.  
  1579.      INTEGER           2 bytes 2's complement
  1580.  
  1581.      CARDINAL          2 bytes
  1582.  
  1583.      LONGCARD          4 bytes
  1584.  
  1585.      LONGINT           4 bytes 2's complement
  1586.  
  1587.      BOOLEAN           1 byte (1=TRUE, 0=FALSE)
  1588.  
  1589.      REAL              4 bytes Intel 8087 format. 
  1590.  
  1591.  
  1592.  
  1593.  
  1594.      The Compiler                                                        28
  1595.      
  1596.  
  1597.      LONGREAL          8 bytes Intel 8087 format. 
  1598.  
  1599.      BITSET            1 word.  0 is low order bit, 15 is high order bit. 
  1600.  
  1601.      Enumerations      1 byte
  1602.  
  1603.      SETs              1 to 8 words (sets of up to 256 elements)
  1604.  
  1605.      POINTERs          4 bytes in Intel 8086/88 format
  1606.  
  1607.      PROCEDUREs        4 bytes POINTER to procedure entry point
  1608.  
  1609.      Addresses are represented in the default Intel 8086 format:
  1610.  
  1611.           1 word          byte offset
  1612.  
  1613.           1 word          segment
  1614.  
  1615.      Numeric values are likewise represented the way the Intel 8086
  1616.      processor family likes them: low order byte first, high order byte
  1617.      last. 
  1618.  
  1619.      
  1620.  
  1621.  
  1622.      4.9.2 The runtime memory map
  1623.  
  1624.      Currently, the compiler generates code using the "large" or "huge"
  1625.      memory model only. 
  1626.  
  1627.      In the "huge" memory model, each module has its own data and code
  1628.      segments. 
  1629.  
  1630.      In the "large" memory model, each module has its own code segment. 
  1631.      The entire program has one data segment. 
  1632.  
  1633.      The linker binds all the code segments first, and then all the data
  1634.      segments.  The stack is allocated above the data segments.  All the
  1635.      remaining memory is available for the heap. 
  1636.  
  1637.      When a program is loaded for execution, here is what the memory looks
  1638.      like:
  1639.  
  1640.  
  1641.           From low to high addresses:
  1642.  
  1643.           0         ----------------------------------------------
  1644.                     I  Interrupt vectors                         I
  1645.                     I  DOS                                       I
  1646.           PSP       I  Program segment prefix                    I
  1647.           PSP+100h  I  Program Code segments                     I
  1648.                     I  Program Data segment(s)                   I
  1649.           StackSeg  I  Stack                                     I
  1650.  
  1651.  
  1652.  
  1653.      The Compiler                                                        29
  1654.      
  1655.  
  1656.           HeapTop   I  Heap                                      I
  1657.                     I  ...                                       I
  1658.                     I  DOS Command (resident portion)            I
  1659.           MemTop    ----------------------------------------------
  1660.  
  1661.      Label names on the left are the ones exported by System.
  1662.  
  1663.      This system uses interrupt vector 192 (0C0H) at location 0000:0300.
  1664.      Interrupt 192 is issued by a program when a runtime error occurs, when
  1665.      HALT is invoked or when a coroutine other than the main one terminates
  1666.      via a return. 
  1667.  
  1668.      The first word (offset 0) in every code segment contains the data
  1669.      segment value for that particular module (for the program, in the case
  1670.      of the "large" memory model. 
  1671.  
  1672.  
  1673.      4.9.3 Procedure calling conventions
  1674.  
  1675.      Procedure parameters are pushed into the stack 1st argument first. 
  1676.      Control is then transferred to the procedure through a FAR call.  It
  1677.      is the called procedure's responsibility to remove its parameters from
  1678.      the stack before returning. 
  1679.  
  1680.      
  1681.  
  1682.  
  1683.      4.9.3.1 Parameter passing (all except open array parameters)
  1684.  
  1685.      If the formal parameter of a procedure is a value parameter, the
  1686.      actual parameter is copied into the stack. 
  1687.  
  1688.      If the formal parameter is a variable parameter (VAR), the address of
  1689.      the actual parameter is pushed into the stack (first the segment
  1690.      portion of the address and then the offset part). 
  1691.  
  1692.      
  1693.  
  1694.  
  1695.      4.9.3.2 Parameter passing (open array parameters)
  1696.  
  1697.      If the formal parameter is an open array, the address and HIGH value
  1698.      of the corresponding formal parameter are pushed into the stack (HIGH
  1699.      value first, and then the address, as above). 
  1700.  
  1701.      If the open array parameter is a value parameter, the value of the
  1702.      actual parameter is copied into the stack on procedure entry. 
  1703.  
  1704.      
  1705.  
  1706.  
  1707.      4.9.3.3 Returning values from a function procedure
  1708.  
  1709.  
  1710.  
  1711.  
  1712.      The Compiler                                                        30
  1713.      
  1714.  
  1715.      One byte results are returned in AL, two byte results are returned in
  1716.      AX, and four byte results are returned in DX:AX (DX has the high order
  1717.      part of the result). 
  1718.  
  1719.      LONGREALs are returned in the stack, at a location reserved for that
  1720.      purpose by the caller.  When invoking a function that returns a
  1721.      LONGREAL, an extra parameter is pushed onto the stack: the two byte
  1722.      offset, in the SS segment, of where to place the result.  This choice
  1723.      will make it easy to allow for the return of arbitrary structures from
  1724.      a function, should the language standard go that way, while at the
  1725.      same time allowing for full reentrancy of the code generated. 
  1726.  
  1727.      
  1728.  
  1729.  
  1730.  
  1731.  
  1732.      4.10 Module priorities
  1733.  
  1734.  
  1735.      Eight module priority levels are supported in this implementation,
  1736.      from 0 (highest priority) to 7 (lowest).
  1737.  
  1738.      Priorities are implemented by masking off, on the 8259 interrupt
  1739.      controller, all the interrupts at or below the current priority
  1740.      level. 
  1741.  
  1742.      Because the PC usually runs with several of the interrupt levels
  1743.      disabled, it is not easy to decide what the interrupt mask for the
  1744.      value for "no priority" should be for your particular application. 
  1745.      The implementation of NEWPROCESS, therefore, assumes that you have
  1746.      enabled all the interrupts that your program will be capable of
  1747.      processing before you create your processes.  The value in the
  1748.      interrupt mask register of the 8259 at the time of process creation
  1749.      will determine the initial priority level of this process, once it
  1750.      gets started.  Because of this, invoking NEWPROCESS from inside a
  1751.      priority module is usually not what you want to do! 
  1752.  
  1753.      Execution priorities are changed when entering/exiting procedures in
  1754.      modules that have a priority specification, and during the execution
  1755.      of some form of a TRANSFER.
  1756.  
  1757.      We highly recommend that you study the communications program
  1758.      provided, paying particular attention to the module Kernel, for an
  1759.      example of how to use priorities with this system. 
  1760.  
  1761.      NOTE: The compiler does not restrict the priority level specified (any
  1762.      number will do).  You must, therefore, exercise care in defining a
  1763.      module's priority level.  On the other hand, it is easy to add
  1764.      additional priority levels by simply modifying the runtime module
  1765.      M2Procs.
  1766.  
  1767.      
  1768.  
  1769.  
  1770.  
  1771.      The Compiler                                                        31
  1772.      
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.      4.11 Memory models
  1779.  
  1780.  
  1781.      In general, you may compile the same code under either the LARGE or
  1782.      the HUGE memory model. 
  1783.  
  1784.      The only factor to consider is when using inline assembler. 
  1785.  
  1786.      Under the HUGE memory model, the compiler generates code to reload DS
  1787.      after any invocation of an imported procedure or a VARiable
  1788.      procedure.  Under the LARGE memory model, this is not necessary as a
  1789.      single data segment is defined.  If you write some inline assembler
  1790.      code that modifies DS, please restore it, even if the next thing you
  1791.      do is a RETurn; this way, your routine will work regardless of whether
  1792.      you use the LARGE or the HUGE memory model. 
  1793.  
  1794.      Under the HUGE memory model, access to external variables is done
  1795.      through an indirect pointer, whereas in the LARGE memory model the
  1796.      external variable resides in the program's ONLY data segment and is,
  1797.      therefore, directly accessible. 
  1798.  
  1799.      
  1800.  
  1801.  
  1802.  
  1803.  
  1804.  
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810.  
  1811.  
  1812.  
  1813.  
  1814.  
  1815.  
  1816.  
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823.  
  1824.  
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830.      Using OBJ files                                                     32
  1831.      
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.                                     Chapter 5
  1840.  
  1841.                                  Using OBJ files
  1842.  
  1843.  
  1844.  
  1845.      As long as you use this system to write Modula-2 programs for the DOS
  1846.      environment only, there is virtually no reason to use OBJ files.  But,
  1847.      in case you have to... 
  1848.  
  1849.      You condition the compiler to generate OBJ files by setting the
  1850.      environment variable M2OUPUT to OBJ.
  1851.  
  1852.           set M2OUTPUT=OBJ
  1853.  
  1854.      To link OBJ files you must exit the MC environment (or invoke the DOS
  1855.      shell) and use some OBJ file linker. 
  1856.  
  1857.      Also, with OBJ files, there is no module version checking done for
  1858.      you.  Of course, if you keep your ".MAK" files up to date, you should
  1859.      not encounter any problems. 
  1860.  
  1861.      
  1862.  
  1863.  
  1864.  
  1865.  
  1866.      5.1 GenLink
  1867.  
  1868.  
  1869.      To help you figure out which files need to be linked, we provide a
  1870.      utility (GenLink) that generates a LINK answer file.  The LINK answer
  1871.      file created by GenLink lists all the Modula-2 OBJ files that are
  1872.      required to link your program. 
  1873.  
  1874.      Since your program, most likely, requires modules written in another
  1875.      language (or you would not be using OBJ files, right?!), you will have
  1876.      to edit the file created by GenLink to make it suitable for the job at
  1877.      hand. 
  1878.  
  1879.      
  1880.  
  1881.  
  1882.  
  1883.  
  1884.      5.2 Foreign Modules
  1885.  
  1886.  
  1887.  
  1888.  
  1889.      Using OBJ files                                                     33
  1890.      
  1891.  
  1892.  
  1893.  
  1894.      To let the Modula-2 compiler know about modules written in other
  1895.      languages, you must write FOREIGN DEFINITION modules.  A foreign
  1896.      definition modules is a regular definition module, with the exception
  1897.      of the module's header, which goes like this:
  1898.  
  1899.           FOREIGN [C|PASCAL] DEFINITION MODULE modName;
  1900.  
  1901.      where the 'C' or 'PASCAL' qualifier is optional. 
  1902.  
  1903.      
  1904.  
  1905.  
  1906.      5.2.1 External names
  1907.  
  1908.      Names of public variables and procedures in foreign modules are
  1909.      encoded in one of 3 ways:
  1910.  
  1911.           In a regular FOREIGN (neither C nor PASCAL) module, the
  1912.           identifiers are encoded in the object files as you enter them.
  1913.  
  1914.           In a FOREIGN C module, identifiers are written out preceded by a
  1915.           '_' character.
  1916.  
  1917.           In a FOREIGN PASCAL module, identifiers are converted to all
  1918.           upper case.
  1919.  
  1920.      WARNING: As currently implemented, SET and STRING constants defined in
  1921.      a FOREIGN DEFINITION module, cause the compiler to generate external
  1922.      references to these.  These "constants" should, therefore, be
  1923.      allocated space and properly initialized in the foreign module. 
  1924.  
  1925.      
  1926.  
  1927.  
  1928.      5.2.2 Implementation
  1929.  
  1930.      FOREIGN modules are not expected to have an initialization procedure,
  1931.      and the compiler will not generate these initialization calls, as in
  1932.      the case of regular Modula-2 modules. 
  1933.  
  1934.      
  1935.  
  1936.  
  1937.      5.2.2.1 FOREIGN C modules
  1938.  
  1939.      When invoking a routine defined in a FOREIGN C module, the arguments
  1940.      are pushed onto the stack in reverse order, as per C's custom.  Also,
  1941.      the caller will remove the arguments off the stack upon return from
  1942.      the subroutine. 
  1943.  
  1944.      Since C, unlike Modula-2, supports (?) the passing of a variable
  1945.  
  1946.  
  1947.  
  1948.      Using OBJ files                                                     34
  1949.      
  1950.  
  1951.      number of arguments to a function, the symbol '...' may be used at the
  1952.      end of a PROCEDURE parameter list definition to indicate that an
  1953.      indeterminate number of arguments may be passed.  Example:
  1954.  
  1955.           PROCEDURE sum( n:INTEGER; ... ) :INTEGER;
  1956.  
  1957.      defines a function that takes n integers and returns their sum.  It
  1958.      could, actually, be implemented in Modula-2 thus:
  1959.  
  1960.           PROCEDURE sum( n:INTEGER; ... ) :INTEGER;
  1961.           VAR p   :POINTER TO INTEGER;
  1962.               res :INTEGER;
  1963.           BEGIN
  1964.               res := 0;
  1965.               p := ADR(n) + 2;
  1966.               WHILE n > 0 DO
  1967.                   res := res + p^;
  1968.                   INC( p, 2 );
  1969.                   DEC( n );
  1970.               END;
  1971.               RETURN res;
  1972.           END sum;
  1973.  
  1974.      Good luck! 
  1975.  
  1976.      In addition to being useful to define functions that take a variable
  1977.      number of parameters, the use of '...' is also a handy (?!) way of
  1978.      improving the odds that the arguments are passed in a form that C will
  1979.      like. 
  1980.  
  1981.      
  1982.  
  1983.  
  1984.      5.2.2.2 Parameter passing
  1985.  
  1986.      The form in which the individual parameters are passed is always the
  1987.      same, regardless of whether the procedure is in a foreign module or
  1988.      not.  The exception is when you use '...'.
  1989.  
  1990.      When passing parameters that correspond to the '...' in the procedure
  1991.      heading, the compiler follows the default C rules: Everything is
  1992.      passed by value, except for arrays, which are passed by reference
  1993.      (their address, instead of their value, is pushed onto the stack). 
  1994.  
  1995.      Modula-2 does not allow functions to return structured types.  Some C
  1996.      compilers return structured values by actually returning a pointer to
  1997.      those values in the DX:AX register pair, which is just the way that
  1998.      Fitted Modula-2 returns pointers!  With these, therefore, you could
  1999.      define
  2000.  
  2001.           struct someStruct cfunct()
  2002.  
  2003.      as
  2004.  
  2005.  
  2006.  
  2007.      Using OBJ files                                                     35
  2008.      
  2009.  
  2010.  
  2011.           TYPE someStructPtr = POINTER TO someStruct;
  2012.           PROCEDURE cfunct() :someStructPointer;
  2013.  
  2014.      
  2015.  
  2016.  
  2017.      5.2.3 In the real world...
  2018.  
  2019.      Knowing the parameter passing conventions used by the compiler you
  2020.      should have no trouble writing assembly language modules to be invoked
  2021.      by Modula-2.
  2022.  
  2023.      With the help provided (FOREIGN C and '...'), it should be easy enough
  2024.      to interface Modula-2 to C. But is it, really?  Not quite! 
  2025.  
  2026.      There are two main problems that you will have to overcome.  One, is
  2027.      the choice of a suitable memory model.  Our LARGE memory model is
  2028.      probably a better choice than HUGE, as some compilers require DS to
  2029.      always point to DGROUP.
  2030.  
  2031.      The other problem, is the set of requirements imposed by each
  2032.      language's runtime system.  Since we provide all the source code for
  2033.      our runtime, your best bet will probably be to modify our system to
  2034.      suit theirs.  Modules that are obvious candidates for "adaptation" are
  2035.      System and Storage; In their current state, they are virtually
  2036.      guaranteed to not work with another vendor's runtime system, and they
  2037.      are a base on which many other library modules depend. 
  2038.  
  2039.      
  2040.  
  2041.  
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.  
  2051.  
  2052.  
  2053.  
  2054.  
  2055.  
  2056.  
  2057.  
  2058.  
  2059.  
  2060.  
  2061.  
  2062.  
  2063.  
  2064.  
  2065.  
  2066.      The Text Editor                                                     36
  2067.      
  2068.  
  2069.  
  2070.  
  2071.  
  2072.  
  2073.  
  2074.  
  2075.                                     Chapter 6
  2076.  
  2077.                                  The Text Editor
  2078.  
  2079.  
  2080.  
  2081.      The text editor included in this package has all the features that you
  2082.      have come to expect from a basic program editor: the ability to
  2083.      insert, delete, move, find and replace text; support for concurrent
  2084.      editing of multiple files (as many as will fit in memory) in separate
  2085.      windows (as many as will fit on the screen) with the ability to copy
  2086.      or move text from window to window. 
  2087.  
  2088.      Although you may load the same file in two different windows, the
  2089.      editor will not be aware of the fact and will treat the two copies as
  2090.      two different files. 
  2091.  
  2092.      The only preset limitation in the editor is that it cannot handle
  2093.      files bigger than 64k. This decision was justified by the fact that
  2094.      Modula-2 programs are supposed to be modular.  File load/save speed
  2095.      was the overriding factor here. 
  2096.  
  2097.      All the text editor keys are defined by the user through the use of
  2098.      the EDCONFIG program.  When the editor starts, it expects to find the
  2099.      file M2ED.CFG in the current PATH.
  2100.  
  2101.      M2ED.CFG will also tell the editor what display colors (attributes) to
  2102.      use for the Status line, for Normal text and for Marked text blocks. 
  2103.  
  2104.      Finally, M2ED.CFG also contains the default settings for the TAB size
  2105.      value, as well as whether or not the editor will expand the tabs
  2106.      inserted into the text spaces.  Note: Tabs present in a file will not
  2107.      be expanded to spaces by the editor. 
  2108.  
  2109.      To get you started, we provide a M2ED.CFG file with the following
  2110.      definitions:
  2111.  
  2112.           Cursor left                    : Left
  2113.           Cursor right                   : Right
  2114.           Cursor up                      : Up
  2115.           Cursor down                    : Down
  2116.           Previous word                  : ^Left
  2117.           Next word                      : ^Righ
  2118.           Page up                        : PgUp
  2119.           Page down                      : PgDn
  2120.           Cursor to beginning of line    : Home
  2121.           Cursor to end of line          : End
  2122.  
  2123.  
  2124.  
  2125.      The Text Editor                                                     37
  2126.      
  2127.  
  2128.           Cursor to top of window        : ^Home
  2129.           Cursor to bottom of window     : ^End
  2130.           To beginning of file           : ^PgUp
  2131.           To end of file                 : ^PgDn
  2132.           Current line to top of window  : AltT
  2133.           Toggle insert/overtype         : Ins
  2134.           Delete character under cursor  : Del
  2135.           Delete previous character      : ^H
  2136.           Delete Current Line            : ^Y
  2137.           Delete to EOL                  : AltY
  2138.           Delete Word                    : ^D
  2139.           Indent Line                    : F4
  2140.           Unindent Line                  : F3
  2141.           Indent Block                   : Alt=
  2142.           Unindent Block                 : Alt-
  2143.           New file                       : AltN
  2144.           Read file                      : AltR
  2145.           Write block                    : AltW
  2146.           Save file                      : AltS
  2147.           Open window                    : ^O
  2148.           Close Window                   : ^C
  2149.           Next window                    : F2
  2150.           Previous window                : aF2
  2151.           Split screen                   : ^S
  2152.           Mark beginning of block        : F7
  2153.           Mark end of block              : F8
  2154.           Goto beginning of block        : AltB
  2155.           Goto end of block              : AltE
  2156.           Clear block marks              : AltH
  2157.           Copy block                     : AltC
  2158.           Delete block                   : AltD
  2159.           Move block                     : AltM
  2160.           Search forward                 : F5
  2161.           Search backwards               : aF5
  2162.           Replace forward                : F6
  2163.           Replace backwards              : aF6
  2164.           Global replace                 : ^F6
  2165.           Repeat last search/replace     : F1
  2166.           Goto next error                : ^E
  2167.           Goto previous error            : ^P
  2168.           Goto line                      : AltG
  2169.           Set options                    : AltO
  2170.           Redraw the screen              : ^L
  2171.           Quit                           : AltQ
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.      The Linker                                                          38
  2185.      
  2186.  
  2187.  
  2188.  
  2189.  
  2190.  
  2191.  
  2192.  
  2193.                                     Chapter 7
  2194.  
  2195.                                    The Linker
  2196.  
  2197.  
  2198.  
  2199.      The linker is invoked by the command line
  2200.  
  2201.           M2LINK myprog [/s n] [/h n] [/o] [/l] [/v] [/swap [path]]
  2202.  
  2203.      where 'myprog' is the main module of the program you are creating. 
  2204.      The options are thus:
  2205.  
  2206.      /s n              n is the size of the stack to allocate (default is
  2207.                        8192).
  2208.  
  2209.      /h n              n is the amount of space to reserve for the heap (in
  2210.                        paragraphs).  The default is all the free memory. 
  2211.  
  2212.      /o                invokes the optimizer.  The optimizer prevents the
  2213.                        output, to the object file, of all the procedures
  2214.                        that are part of included modules but are not
  2215.                        referenced.  This will make your final EXE files
  2216.                        smaller.  This option (/o) and /l are mutually
  2217.                        exclusive; The last one seen on the command line is
  2218.                        the one that takes precedence. 
  2219.  
  2220.      /l                tells the linker to process the line number
  2221.                        information in the .M2O files and include it in the
  2222.                        .DBG file.  This option (/l) and /o are mutually
  2223.                        exclusive; The last one seen on the command line is
  2224.                        the one that takes precedence. 
  2225.  
  2226.      /k                tells the linker to ignore module keys, i.e.  to not
  2227.                        check for module version compatibility.  This option
  2228.                        should be used with extreme care. 
  2229.  
  2230.      /v                enables verbose mode (which was the default in older
  2231.                        versions of the linker). 
  2232.  
  2233.      /swap [path]      tells the linker to use a swap file.  Code segments
  2234.                        will be kept in this swap file instead of in main
  2235.                        memory during the link process.  This allows you to
  2236.                        link larger programs. 
  2237.  
  2238.      The linker creates two files: the .EXE file is your executable
  2239.      program, the .DBG file is a file containing symbol information for use
  2240.  
  2241.  
  2242.  
  2243.      The Linker                                                          39
  2244.      
  2245.  
  2246.      by other utilities (see Map file generator, Profiler).
  2247.  
  2248.      If the /swap directive is used, a swap file is created.  You may
  2249.      select the path (drive:directory) where the swap file is to be
  2250.      created.  Example:
  2251.  
  2252.           M2LINK myprog /swap D:
  2253.  
  2254.  
  2255.  
  2256.  
  2257.      7.1 Module keys
  2258.  
  2259.  
  2260.      The module header record and the import records written out by the
  2261.      compiler to the object file are stamped with the date of the .DEF file
  2262.      that was processed - this becomes the module key.  The linker will
  2263.      assure that these module keys in the module header of the imported
  2264.      module and in the import record match; If they do not match, both
  2265.      modules were not compiled using the same definition module. 
  2266.  
  2267.      Because of the use of module keys, it is imperative that the date of
  2268.      the distributed .DEF files not be modified unless you intend to
  2269.      recompile the implementation modules. 
  2270.  
  2271.      When using OBJ files, you do not have the protection of Module keys. 
  2272.  
  2273.      
  2274.  
  2275.  
  2276.  
  2277.  
  2278.  
  2279.  
  2280.  
  2281.  
  2282.  
  2283.  
  2284.  
  2285.  
  2286.  
  2287.  
  2288.  
  2289.  
  2290.  
  2291.  
  2292.  
  2293.  
  2294.  
  2295.  
  2296.  
  2297.  
  2298.  
  2299.  
  2300.  
  2301.  
  2302.      Other utilities                                                     40
  2303.      
  2304.  
  2305.  
  2306.  
  2307.  
  2308.  
  2309.  
  2310.  
  2311.                                     Chapter 8
  2312.  
  2313.                                  Other utilities
  2314.  
  2315.  
  2316.  
  2317.      
  2318.  
  2319.  
  2320.  
  2321.  
  2322.      8.1 Editor configurator
  2323.  
  2324.  
  2325.      This program lets you define the keystrokes to be used to invoke all
  2326.      the editor commands, the screen attributes (colors) to use, and the
  2327.      default editor options. 
  2328.  
  2329.      Invocation:
  2330.  
  2331.           EDCONFIG
  2332.  
  2333.      EdConfig presents you with a menu from which you may elect to modify
  2334.      the default editor options, the display attributes, or configure the
  2335.      editor commands. 
  2336.  
  2337.      If you are creating a new configuration file, you must configure the
  2338.      editor commands. 
  2339.  
  2340.      If you chose to configure the editor commands, you will be prompted
  2341.      for a log file (default M2ED.HLP), a text file in which all of your
  2342.      command choices will be saved.  You may print this file to create a
  2343.      quick reference card.  EDCONFIG will then prompt you for the key
  2344.      sequence to be used for each editor command.  For each command,
  2345.      EDCONFIG will also give you the option of defining an alternate key
  2346.      sequence. 
  2347.  
  2348.      When you select Quit, the program will prompt you for an output file,
  2349.      the default being 'M2ED.CFG'.
  2350.  
  2351.      
  2352.  
  2353.  
  2354.  
  2355.  
  2356.      8.2 Map file generator
  2357.  
  2358.  
  2359.  
  2360.  
  2361.      Other utilities                                                     41
  2362.      
  2363.  
  2364.  
  2365.  
  2366.      Invocation:
  2367.  
  2368.           DBG2MAP program_name
  2369.  
  2370.      Reads the .DBG file created by M2LINK and creates a DOS LINK
  2371.      compatible .MAP file. 
  2372.  
  2373.      
  2374.  
  2375.  
  2376.  
  2377.  
  2378.      8.3 Make and the Makefile generator
  2379.  
  2380.  
  2381.      These utilities eliminate the burden, on the user's part, of having to
  2382.      figure out which modules are affected and, therefore, need to be
  2383.      recompiled as a result of any changes to particular definition
  2384.      modules. 
  2385.  
  2386.      GENMAKE creates the .MAK file, the file with all the dependencies
  2387.      (this is the hard part) whereas MAKE (built into the compiler) will
  2388.      insure that these dependencies are observed when updating the object
  2389.      files. 
  2390.  
  2391.      GENMAKE Invocation:
  2392.  
  2393.           GENMAKE main_module_name [/l] [/obj]
  2394.  
  2395.      generates the .MAK file containing all the module dependencies for the
  2396.      named program.  It does this by reading all the IMPORT statements in
  2397.      the main module and, recursively, generating the dependency lists for
  2398.      all those modules.  GENMAKE will indeed read all the .MOD and .DEF
  2399.      files involved. 
  2400.  
  2401.      The /l option instructs GenMake to include the directories listed in
  2402.      the environment variable M2LIB in its search path.  Otherwise, only
  2403.      the modules in the current directory will be taken into consideration
  2404.      when creating the makefile. 
  2405.  
  2406.      The /obj option tells GenMake that you will be using the compiler to
  2407.      generate OBJ files instead of M2O files. 
  2408.  
  2409.      MAKE invocation: see "Running the Compiler".
  2410.  
  2411.      MAKE will invoke the compiler as needed to assure that all the
  2412.      dependencies in the make file are observed.  MAKE is dumb in that it
  2413.      will just run through the makefile sequentially.  It was GENMAKE's
  2414.      responsibility to see that the dependencies are listed in a proper
  2415.      sequence.  Please keep this in mind if you should edit a makefile! 
  2416.  
  2417.  
  2418.  
  2419.  
  2420.      Other utilities                                                     42
  2421.      
  2422.  
  2423.      
  2424.  
  2425.  
  2426.  
  2427.  
  2428.      8.4 M2O file decoder
  2429.  
  2430.  
  2431.      Invocation:
  2432.  
  2433.           DECODE module_name
  2434.  
  2435.      Decodes a .M2O file sending the output to the terminal. 
  2436.  
  2437.      
  2438.  
  2439.  
  2440.  
  2441.  
  2442.      8.5 The execution profiler
  2443.  
  2444.  
  2445.      Invocation:
  2446.  
  2447.           M2PROF program_name
  2448.  
  2449.      The profiler will ask you for the name of the .DBG file to use (if it
  2450.      cannot find it) and give you an option of profiling your entire
  2451.      program (generating an execution profile by module), a particular
  2452.      module (generating an execution profile by procedure in that module)
  2453.      or a particular procedure (generating an execution profile by line in
  2454.      the procedure). 
  2455.  
  2456.      Upon program termination, the profiler outputs the list of all the
  2457.      modules/procedures/lines profiled, ranked by execution time, to a file
  2458.      of your choice. 
  2459.  
  2460.      This profiler is not that versatile, but it is useful nevertheless. 
  2461.      It proved instrumental in pinpointing some obvious areas for
  2462.      improvement in the compiler (Oh, we did not tell you, did we?  This
  2463.      compiler was written in the language it compiles -- Modula-2 -- and
  2464.      this system was used as our primary development tool since very early
  2465.      in the development process). 
  2466.  
  2467.      
  2468.  
  2469.  
  2470.  
  2471.  
  2472.  
  2473.  
  2474.  
  2475.  
  2476.  
  2477.  
  2478.  
  2479.      The Library Modules                                                 43
  2480.      
  2481.  
  2482.  
  2483.  
  2484.  
  2485.  
  2486.  
  2487.  
  2488.                                     Chapter 9
  2489.  
  2490.                                The Library Modules
  2491.  
  2492.  
  2493.  
  2494.      For complete information on what each library module provides, as well
  2495.      as its proper usage, please refer to the .DEF files. 
  2496.  
  2497.      In addition, the source code of all the library modules is available
  2498.      to all the registered users (see the order form in the back of this
  2499.      document for details). 
  2500.  
  2501.      
  2502.  
  2503.  
  2504.  
  2505.  
  2506.      9.1 Release 3.0 libraries
  2507.  
  2508.  
  2509.      We have not created any source level incompatibility with previous
  2510.      library modules, that we are aware of.  But you will have to recompile
  2511.      all your programs, as they will not link with the new library
  2512.      modules. 
  2513.  
  2514.      
  2515.  
  2516.  
  2517.  
  2518.  
  2519.  
  2520.  
  2521.  
  2522.  
  2523.  
  2524.  
  2525.  
  2526.  
  2527.  
  2528.  
  2529.  
  2530.  
  2531.  
  2532.  
  2533.  
  2534.  
  2535.  
  2536.  
  2537.  
  2538.      The runtime support system                                          44
  2539.      
  2540.  
  2541.  
  2542.  
  2543.  
  2544.  
  2545.  
  2546.  
  2547.                                    Chapter 10
  2548.  
  2549.                            The runtime support system
  2550.  
  2551.  
  2552.  
  2553.      The library module System contains the runtime system's initialization
  2554.      code.  In particular, special interrupt vectors are loaded with the
  2555.      addresses of routines (also in System) that will handle runtime errors
  2556.      and other abnormal program terminations.  In addition, the module
  2557.      Storage depends on System doing its stuff -- setting the HeapBase,
  2558.      HeapTop and MemTop variables. 
  2559.  
  2560.      Three other special modules are included in this package.  M2Reals,
  2561.      M2Longs and M2Procs. These modules provide support for specific
  2562.      language features. 
  2563.  
  2564.      M2Reals handles all the REAL and LONGREAL arithmetic and conversions. 
  2565.      In release 2, M2Reals checks for the presence of an 8087 math
  2566.      co-processor and uses it, if found. 
  2567.  
  2568.      M2Longs handles LONGINT and LONGCARD arithmetic. 
  2569.  
  2570.      M2Procs provides the coroutine support. 
  2571.  
  2572.  
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578.  
  2579.  
  2580.  
  2581.  
  2582.  
  2583.  
  2584.  
  2585.  
  2586.  
  2587.  
  2588.  
  2589.  
  2590.  
  2591.  
  2592.  
  2593.  
  2594.  
  2595.  
  2596.  
  2597.      Modula-2 Syntax                                                     45
  2598.      
  2599.  
  2600.  
  2601.  
  2602.  
  2603.  
  2604.  
  2605.  
  2606.                                    Chapter 11
  2607.  
  2608.                                  Modula-2 Syntax
  2609.  
  2610.  
  2611.  
  2612.  
  2613.      ident = letter {letter | digit}.
  2614.      number = integer | real.
  2615.      integer = digit {digit} | octalDigit {octalDigit} ("B"|"C") |
  2616.                digit {hexDigit} "H".
  2617.      real = digit {digit} "." {digit} {ScaleFactor}.
  2618.      ScaleFactor = "E" ["+"|"-"] digit {digit}.
  2619.      hexDigit = digit | "A" | "B" | "C" | "D" | "E" | "F".
  2620.      digit = octalDigit | "8" | "9".
  2621.      octalDigit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7".
  2622.      string = "'" {character} "'" | '"' {character} '"' .
  2623.      qualident = ident {"." ident}.
  2624.      ConstantDeclaration = ident "=" ConstExpression.
  2625.      ConstExpression = expression.
  2626.      TypeDeclaration = ident "=" type.
  2627.      type = SimpleType | ArrayType | RecordType | SetType |
  2628.             PointerType | ProcedureType.
  2629.      SimpleType = qualident | enumeration | SubrangeType.
  2630.      enumeration = "(" IdentList ")".
  2631.      IdentList = ident {"," ident}.
  2632.      SubrangeType = [ident] "[" ConstExpression ".." ConstExpression "]".
  2633.      ArrayType = ARRAY SimpleType {"," SimpleType} OF type.
  2634.      RecordType = RECORD FieldListSequence END.
  2635.      FieldListSequence = FieldList {";" FieldList}.
  2636.      FieldList = [IdentList ":" type |
  2637.                  CASE [ident] ":" qualident OF variant {"|" variant}
  2638.                  [ELSE FieldListSequence] END].
  2639.      variant = [CaseLabelList ":" FieldListSequence].
  2640.      CaseLabelList = CaseLabels {"," CaseLabels}.
  2641.      CaseLabels = ConstExpression [".." ConstExpression].
  2642.      SetType = SET OF SimpleType.
  2643.      PointerType = POINTER TO type.
  2644.      ProcedureType = PROCEDURE [FormalTypeList].
  2645.      FormalTypeList = "(" [ [VAR] FormalType
  2646.                       {"," [VAR] FormalType} ] ")" [":" qualident].
  2647.      VariableDeclaration = IdentList ":" type.
  2648.      designator = qualident {"." ident | "[" ExpList "]" | "^"}.
  2649.      ExpList = expression {"," expression}.
  2650.      expression = SimpleExpression [relation SimpleExpression].
  2651.      relation = "=" | "#" | "<" |"<=" | ">" | ">=" | IN.
  2652.      SimpleExpression = ["+"|"-"] term {AddOperator term}.
  2653.  
  2654.  
  2655.  
  2656.      Modula-2 Syntax                                                     46
  2657.      
  2658.  
  2659.      AddOperator = "+" | "-" | OR.
  2660.      term = factor {MulOperator factor}.
  2661.      MulOperator = "*" |"/" | DIV | MOD | AND.
  2662.      factor = number | string | set | designator [ActualParameters] |
  2663.              "(" expression ")" | NOT factor.
  2664.      set = [qualident] "{" [element {"," element}] "}".
  2665.      element = expression [".." expression].
  2666.      ActualParameters = "(" [ExpList] ")" .
  2667.      statement = [assignment | ProcedureCall |
  2668.                  IfStatement | CaseStatement | WhileStatement |
  2669.                  RepeatStatement | LoopStatement | ForStatement |
  2670.                  WithStatement | EXIT | RETURN [expression] ].
  2671.      assignment = designator ":=" expression.
  2672.      ProcedureCall = designator [ActualParameters].
  2673.      StatementSequence = statement {";" statement}.
  2674.      IfStatement = IF expression THEN StatementSequence
  2675.                    {ELSIF expression THEN StatementSequence}
  2676.                    [ELSE StatementSequence] END.
  2677.      CaseStatement = CASE expression OF case {"|" case}
  2678.                      [ELSE StatementSequence] END.
  2679.      case = [CaseLabelList ":" StatementSequence].
  2680.      WhileStatement = WHILE expression DO StatementSequence END.
  2681.      RepeatStatement = REPEAT StatementSequence UNTIL expression.
  2682.      ForStatement = FOR ident ":=" expression TO expression
  2683.                     [BY ConstExpression] DO StatementSequence END.
  2684.      LoopStatement = LOOP StatementSequence END.
  2685.      WithStatement = WITH designator DO StatementSequence END .
  2686.      ProcedureDeclaration = ProcedureHeading ";" block ident.
  2687.      ProcedureHeading = PROCEDURE ident [FormalParameters].
  2688.      block = {declaration} [BEGIN StatementSequence] END.
  2689.      declaration = CONST {ConstantDeclaration ";"} |
  2690.                    TYPE {TypeDeclaration ";"} |
  2691.                    VAR {VariableDeclaration ";"} |
  2692.                    ProcedureDeclaration ";" | ModuleDeclaration ";".
  2693.      FormalParameters =
  2694.               "(" [FPSection {";" FPSection}] ")" [":" qualident].
  2695.      FPSection = [VAR] IdentList ":" FormalType.
  2696.      FormalType = [ARRAY OF] qualident.
  2697.      ModuleDeclaration =
  2698.          MODULE ident [priority] ";" [import] [export] block ident.
  2699.      priority = "[" ConstExpression "]".
  2700.      export = EXPORT [QUALIFIED] IdentList ";".
  2701.      import = [FROM ident] IMPORT IdentList ";".
  2702.      DefinitionModule = DEFINITION MODULE ident ";"
  2703.                         {import} {definition} END ident "." .
  2704.      definition = CONST {ConstantDeclaration ";"} |
  2705.                   TYPE {ident ["=" type] ";"} |
  2706.                   VAR {VariableDeclaration ";"} |
  2707.                   ProcedureHeading ";".
  2708.      ProgramModule = MODULE ident [priority] ";" {import} block ident ".".
  2709.      CompilationUnit = DefinitionModule | [IMPLEMENTATION] ProgramModule.
  2710.  
  2711.      
  2712.  
  2713.  
  2714.  
  2715.      Classes                                                             47
  2716.      
  2717.  
  2718.  
  2719.  
  2720.  
  2721.  
  2722.  
  2723.  
  2724.                                    Chapter 12
  2725.  
  2726.                                      Classes
  2727.  
  2728.  
  2729.  
  2730.      Version 3 of the compiler has extensions intended to add support for
  2731.      object oriented programming (OOP) through the introduction of
  2732.      classes. 
  2733.  
  2734.      These extensions to Modula-2 should not, of course, be used if program
  2735.      portability to other environments is desired. 
  2736.  
  2737.      This design has not been widely tested, and is subject to change in
  2738.      future versions of the system. 
  2739.  
  2740.      If you use classes, please share your experiences with us. 
  2741.  
  2742.      
  2743.  
  2744.  
  2745.  
  2746.  
  2747.      12.1 Highlights of the implementation
  2748.  
  2749.  
  2750.      Readers already familiar with OOP terminology will be keen to note the
  2751.      highlights of this implementation:
  2752.  
  2753.           OOP features are supported by the use of four new reserved words
  2754.           (CLASS, INHERIT, INIT and DESTROY), two new standard identifiers
  2755.           (SELF and MEMBER), and the Objects library module. 
  2756.  
  2757.           Class definitions may appear in definition modules, leaving the
  2758.           details of the implementations to implementation modules. 
  2759.  
  2760.           Class implementations may extend the class definition, adding new
  2761.           attributes and methods to the original definition. 
  2762.  
  2763.           Only single inheritance is allowed (INHERIT) in this release. 
  2764.  
  2765.           Methods declared in a class definition are virtual. 
  2766.  
  2767.           Methods declared in a class implementation are static. 
  2768.  
  2769.           All objects are dynamic, i.e.  you must create all objects before
  2770.           using them.  There is no automatic garbage collection, i.e.  you
  2771.  
  2772.  
  2773.  
  2774.      Classes                                                             48
  2775.      
  2776.  
  2777.           must explicitly dispose of objects when no longer required. 
  2778.  
  2779.           Class implementations may define object constructor (INIT) and
  2780.           destructor (DESTROY) methods that are invoked automatically when
  2781.           an object is created (by NEW) or destroyed (by DISPOSE).
  2782.  
  2783.           Local classes may be implemented within program and
  2784.           implementation modules.  In such cases, the class implementation
  2785.           is also its definition (in an analogous manner to local modules
  2786.           not requiring a separate definition). 
  2787.  
  2788.           Classes cannot, however, be implemented inside local (nested)
  2789.           modules. 
  2790.  
  2791.      
  2792.  
  2793.  
  2794.  
  2795.  
  2796.      12.2 Classes, attributes, methods, inheritance and polymorphism
  2797.  
  2798.  
  2799.      This section aims to give a very brief introduction to the terminology
  2800.      and ideas of object oriented programming (OOP) for new readers. 
  2801.  
  2802.      Support for an "object" oriented paradigm of programming in Modula-2
  2803.      at the lowest level consists of allowing you to define object "types"
  2804.      (record, arrays, ...) and then to write procedures that manipulate
  2805.      parameters of those classes of objects.  Thus we might have
  2806.  
  2807.           TYPE
  2808.            Gender = (male,female);
  2809.            Person = RECORD
  2810.                       name : ARRAY [0..40] OF CHAR;
  2811.                       sex  : Gender;
  2812.                     END;
  2813.  
  2814.           PROCEDURE IsMale (Human : Person) : BOOLEAN;
  2815.            BEGIN
  2816.              RETURN Human.sex = male
  2817.            END IsMale;
  2818.  
  2819.      In Modula-2 in particular, it is common to find all these related
  2820.      definitions encapsulated in a module.  Indeed, one can go further, and
  2821.      hide the details of the type within the implementation; the library
  2822.      module Windows, for example, defines an opaque type Window and a bunch
  2823.      of procedures that operate on objects of that type (OpenWindow,
  2824.      SelectWindow, ... ).
  2825.  
  2826.      Once one has defined a type like Person, one can go on to use it in
  2827.      the definition of other types
  2828.  
  2829.           TYPE
  2830.  
  2831.  
  2832.  
  2833.      Classes                                                             49
  2834.      
  2835.  
  2836.            Programmer = RECORD
  2837.               who : Person;
  2838.               favoriteLanguage : ARRAY [0..10] OF CHAR;
  2839.            END;
  2840.  
  2841.      and introduce other procedures to manipulate parameters of the
  2842.      Programmer type.  But the awkward fact remains that, while in real
  2843.      life programmers are also persons, in Modula-2 variables of Programmer
  2844.      type are inherently incompatible with variables of Person type. 
  2845.      Furthermore, if one wishes to hide the details of the types by
  2846.      defining them as opaque types, the mechanisms allowed depend heavily
  2847.      on awkward pointer manipulations. 
  2848.  
  2849.      Thus it can be said that the methods traditionally used in Modula-2
  2850.      for program decomposition, while reducing the complexity of a total
  2851.      system, often tend to produce building blocks that are specific to the
  2852.      program at hand, and are reusable for other purposes only after a lot
  2853.      of extra effort has been put into modifying them.  Re-using components
  2854.      often requires many textual changes, which can lead to an entire
  2855.      family of "similar" components that must all be managed and
  2856.      maintained. 
  2857.  
  2858.      Enter the new OOP ideas!  Better support for the paradigm is realized
  2859.      by extending the idea of a RECORD type to the idea of a CLASS type
  2860.  
  2861.      A Class is a new user defined type.  A class defines both the
  2862.      "attributes" (fields) and "methods" (procedures) of the "instances"
  2863.      (objects) of that class; a CLASS declaration combines the declaration
  2864.      of an object's structure with the declaration of all the procedures
  2865.      (methods, in OOP terminology) that deal with that kind of object.  An
  2866.      OOPed version of FileSystem might be introduced along these lines:
  2867.  
  2868.           DEFINITION MODULE OOPFileSystem;
  2869.             FROM SYSTEM IMPORT WORD;
  2870.             TYPE
  2871.               Response = ( done, notdone );
  2872.               IOMode   = ( read, write, io );
  2873.             CLASS File;
  2874.               id   :INTEGER;
  2875.               res  :Response;
  2876.               eof  :BOOLEAN;
  2877.               mode :IOMode;
  2878.               PROCEDURE Lookup(
  2879.                   filename :ARRAY OF CHAR; new :BOOLEAN
  2880.                   );
  2881.               PROCEDURE ReadWord( VAR w :WORD );
  2882.               PROCEDURE WriteWord( w :WORD );
  2883.               (*...*)
  2884.             END File;
  2885.           END OOPFileSystem.
  2886.  
  2887.      NOTE: In FileSystem.File there if a field 'fdptr' which is used to
  2888.      hold implementation specific information.  This is not needed in
  2889.  
  2890.  
  2891.  
  2892.      Classes                                                             50
  2893.      
  2894.  
  2895.      OOPFileSystem.File because we can add the needed fields to File in its
  2896.      implementation. 
  2897.  
  2898.      This code actually gives the class definition rather than the class
  2899.      implementation -- the implementation would be elaborated in a
  2900.      corresponding implementation module.  As usual, the definition gives
  2901.      all we need to know to be able to start using the class -- a program
  2902.      using OOPFileSystem might have code like:
  2903.  
  2904.           FROM OOPFileSystem IMPORT File;
  2905.             VAR
  2906.               f :File;
  2907.               w :WORD;
  2908.             BEGIN
  2909.               NEW(f); (* create an object of the File class *)
  2910.               f.Lookup( "myfile.txt", FALSE ); (* open file *)
  2911.               IF f.res = done THEN
  2912.                   f.ReadWord( w );
  2913.               ...
  2914.  
  2915.      However, notice how we coded "f.ReadWord(w)" instead of
  2916.      "ReadWord(f,w)", and how we had to go further than just declare an
  2917.      instance f of the class File; we had to create the instance by calling
  2918.      NEW(f).
  2919.  
  2920.      So far this does not seem to have bought us much other than a
  2921.      syntactic change, writing code like f.ReadWord(w) in place of
  2922.      ReadWord(f, w).  However, a very real advantage comes from being able
  2923.      to extend classes.  Consider a class definition for a Person
  2924.  
  2925.           TYPE Gender = (unknown,male,female);
  2926.           CLASS Person;
  2927.             name    :ARRAY [0..40] OF CHAR;
  2928.             sex     :Gender;
  2929.             PROCEDURE isMale() :BOOLEAN;
  2930.           END Person;
  2931.  
  2932.      Notice that the method takes no parameter: the method will "know"
  2933.      about the object already, in distinction to the style used in writing
  2934.      traditional procedures. 
  2935.  
  2936.      Programmers are also people, but they have further attributes and
  2937.      methods for dealing with them:
  2938.  
  2939.           TYPE LanguageName = ARRAY [0..10] OF CHAR;
  2940.           CLASS Programmer;
  2941.             INHERIT Person;
  2942.             favoriteLanguage :LanguageName;
  2943.             PROCEDURE isSmart() :BOOLEAN;
  2944.           END Programmer;
  2945.  
  2946.      Note the INHERIT clause: an object of the Programmer class is going to
  2947.      have all the attributes of a Person, and be subjectable to the methods
  2948.  
  2949.  
  2950.  
  2951.      Classes                                                             51
  2952.      
  2953.  
  2954.      that can be applied to a Person -- as well as having new properties of
  2955.      its own.  The Programmer class is said to be based on the Person
  2956.      class, or to be a "sub-class" or descendent of the Person
  2957.      "super-class" or ancestor.. 
  2958.  
  2959.      This implies that an object of the Programmer class is compatible to
  2960.      some degree with an object of a Person class -- even though it appears
  2961.      to be "bigger"!
  2962.  
  2963.      This guaranteed compatibility of derived data types with their base
  2964.      types lies behind the realization of an idea known as polymorphism. 
  2965.      Some variables must be able to assume values of different (but
  2966.      related) data types at run time and, in the course of operations with
  2967.      objects, there must be the possibility of determining at run time the
  2968.      concrete actions to be executed (depending on the current dynamic data
  2969.      type of the object). 
  2970.  
  2971.      All of this represents quite a departure from the very strict typing
  2972.      philosophy of Modula-2.
  2973.  
  2974.      Activating an operation with an object is often termed "sending a
  2975.      message to the object".  The object reacts by executing a method.  The
  2976.      assignment of methods to messages is determined for each class by the
  2977.      respective class definition, and the effect of sending a message
  2978.      differs from procedure invocations in conventional programming
  2979.      languages in that the determination of which method is to be executed
  2980.      can (and often must) occur at run time, rather than at compile time. 
  2981.  
  2982.      
  2983.  
  2984.  
  2985.  
  2986.  
  2987.      12.3 Defining classes in definition modules
  2988.  
  2989.  
  2990.      Time for some specifics that apply to this release of the compiler. 
  2991.  
  2992.      A Class definition may appear in a definition module, where it
  2993.      specifies the format of the data to be allocated when an object of
  2994.      that class is later instantiated, and a set of methods (procedures)
  2995.      that are available to manipulate that object. 
  2996.  
  2997.      A class definition is analogous to a type definition; the extended
  2998.      syntax for definitions in a definition module is described by
  2999.  
  3000.           definition = "CONST" {ConstantDeclaration ";"} |
  3001.                        "TYPE" {ident ["=" type] ";"} |
  3002.                        "VAR" {VariableDeclaration ";"} |
  3003.                        "CLASS" ClassDefinition ";" |
  3004.                        ProcedureHeading ";".
  3005.  
  3006.           ClassDefinition =
  3007.  
  3008.  
  3009.  
  3010.      Classes                                                             52
  3011.      
  3012.  
  3013.                       className ";"
  3014.                       [ "INHERIT" someClassName; ]
  3015.                       [ AttributeDeclarations ]
  3016.                       { MethodDefinition }
  3017.                       "END" className .
  3018.  
  3019.           AttributeDeclaration = { VariableDeclaration ";" } .
  3020.  
  3021.           MethodDefinition = ProcedureHeading .
  3022.           className = ident .
  3023.           someClassName = ident .
  3024.  
  3025.      An example of a Definition Module incorporating CLASS definitions is:
  3026.  
  3027.           DEFINITION MODULE People;
  3028.             TYPE Gender = (unknown,male,female);
  3029.             CLASS Person;
  3030.               name    :ARRAY [0..40] OF CHAR;
  3031.               sex     :Gender;
  3032.               PROCEDURE isMale() :BOOLEAN;
  3033.             END Person;
  3034.  
  3035.             TYPE  LanguageName = ARRAY [0..10] OF CHAR;
  3036.             CLASS Programmer;
  3037.               INHERIT Person;
  3038.               favoriteLanguage :LanguageName;
  3039.               PROCEDURE isSmart() :BOOLEAN;
  3040.             END Programmer;
  3041.           END People.
  3042.  
  3043.      Notes:
  3044.  
  3045.        1.  The attributes are effectively the data fields of the class
  3046.            (object).
  3047.  
  3048.        2.  Methods are effectively procedures and, therefore, defined as
  3049.            such in an analogous way to defining other procedures exported
  3050.            from the module. 
  3051.  
  3052.        3.  The methods are technically known as "virtual methods".  An
  3053.            implementation of each one must be declared in the corresponding
  3054.            implementation module; however, this implementation may be
  3055.            overridden in classes derived from the one being defined. 
  3056.  
  3057.        4.  An attribute of a class may be defined in terms of the class
  3058.            itself. 
  3059.  
  3060.      For examples of this, please refer to the Lists example mentioned at
  3061.      the end of this chapter. 
  3062.  
  3063.      
  3064.  
  3065.  
  3066.  
  3067.  
  3068.  
  3069.      Classes                                                             53
  3070.      
  3071.  
  3072.  
  3073.  
  3074.  
  3075.  
  3076.      12.4 Implementing classes in implementation modules
  3077.  
  3078.  
  3079.      A class defined in a definition module must, of course, be elaborated
  3080.      (its methods fully declared) in the corresponding implementation
  3081.      module.  An implementation module or program module may also declare
  3082.      local classes. 
  3083.  
  3084.      The syntax of declarations in this extension of Modula-2 is described
  3085.      by
  3086.  
  3087.           declaration =
  3088.               CONST {ConstantDeclaration ";"} |
  3089.               TYPE {TypeDeclaration ";"} |
  3090.               VAR {VariableDeclaration ";"} |
  3091.               "CLASS" [ ClassImplementation | ClassDeclaration ] ";" |
  3092.               ProcedureDeclaration ";" |
  3093.               ModuleDeclaration ";".
  3094.  
  3095.           ClassImplementation =
  3096.               className;
  3097.               [ AttributeDeclarations ]
  3098.               { MethodImplementation }
  3099.               [ "INIT" StatementSequence ]
  3100.               [ "DESTROY" StatementSequence ]
  3101.               "END" className .
  3102.  
  3103.           ClassDeclaration =
  3104.               className;
  3105.               [ "INHERIT" someClassName; ]
  3106.               [ AttributeDeclarations ]
  3107.               { MethodImplementation }
  3108.               [ "INIT" StatementSequence ]
  3109.               [ "DESTROY" StatementSequence ]
  3110.               "END" className .
  3111.  
  3112.           MethodImplementation = ProcedureDeclaration .
  3113.  
  3114.      An example of an Implementation Module incorporating CLASS definitions
  3115.      is:
  3116.  
  3117.           IMPLEMENTATION MODULE People;
  3118.             FROM Strings IMPORT CompareStr, Assign;
  3119.  
  3120.             CLASS Person;    (* a class implementation *)
  3121.               PROCEDURE isMale() :BOOLEAN;
  3122.                 BEGIN
  3123.                   RETURN sex = male;
  3124.                 END isMale;
  3125.  
  3126.  
  3127.  
  3128.      Classes                                                             54
  3129.      
  3130.  
  3131.               INIT
  3132.                 name := "";
  3133.                 sex := unknown;
  3134.            END Person;
  3135.  
  3136.             CLASS Programmer;  (* a class implementation *)
  3137.               PROCEDURE isSmart() :BOOLEAN;
  3138.                 BEGIN
  3139.                   RETURN CompareStr(favoriteLanguage,"Modula-2") = 0;
  3140.                 END isSmart;
  3141.               INIT
  3142.                 favoriteLanguage := "?";
  3143.             END Programmer;
  3144.  
  3145.             CLASS Vendor;      (* a local class declaration *)
  3146.               INHERIT Programmer;
  3147.               BusinessAddress : ARRAY [0..40] OF CHAR;
  3148.               PROCEDURE GetAddress (VAR Address : ARRAY OF CHAR);
  3149.                 BEGIN
  3150.                   Assign(BusinessAddress, Address)
  3151.                 END GetAddress;
  3152.               INIT
  3153.                 BusinessAddress := "PO Box 867403, Plano, Texas"
  3154.             END Vendor;
  3155.           END People.
  3156.  
  3157.      Notes:
  3158.  
  3159.        1.  Local classes may be declared in program modules or
  3160.            implementation modules only at the outermost level, i.e.  they
  3161.            may not be declared in local (nested) modules. 
  3162.  
  3163.        2.  Further attributes may be added in a class implementation to
  3164.            those already defined in the class definition as it appears in
  3165.            the definition module. 
  3166.  
  3167.        3.  Direct access to the attributes exported from the class
  3168.            definition is allowed, but should be avoided -- rather provide
  3169.            methods for accessing the attributes safely. 
  3170.  
  3171.        4.  Inside a method, you have full access (with no need for
  3172.            qualification) to the fields of the object that you are dealing
  3173.            with. 
  3174.  
  3175.        5.  When a method is called, the reference to the object on behalf
  3176.            of which the method will operate forms part of the message.  If
  3177.            you need it, you may access this object's reference thru the
  3178.            predefined object handle SELF.
  3179.  
  3180.        6.  Thus in isSmart, above, we could have used SELF
  3181.  
  3182.      
  3183.  
  3184.  
  3185.  
  3186.  
  3187.      Classes                                                             55
  3188.      
  3189.  
  3190.           RETURN CompareStr(SELF.favoriteLanguage,"Modula-2") = 0;
  3191.  
  3192.           but that is not necessary in this case.  You would, typically,
  3193.           use SELF when you want to pass your object reference to a method
  3194.           of another class, as a parameter.  We show an example of this in
  3195.           our Lists example. 
  3196.  
  3197.      
  3198.  
  3199.  
  3200.  
  3201.  
  3202.      12.5 Object instantiation
  3203.  
  3204.  
  3205.      We shall use the term "object handle" to describe the names given to
  3206.      instances of a class -- instances known as "objects". An object handle
  3207.      holds a reference (call it a pointer if you must) to an object
  3208.      instance. 
  3209.  
  3210.      You declare an object handle just as you would declare a variable,
  3211.      using for its type the name of the class (of objects) that this handle
  3212.      will refer to.  For example:
  3213.  
  3214.           VAR myBoss :Person;
  3215.  
  3216.      However, since in this implementation all objects are "dynamic",
  3217.      before you can use an object you must instantiate (create) it via a
  3218.      call to the standard procedure NEW, passing NEW the handle as a
  3219.      parameter.  (This is somewhat analogous to using NEW to allocate
  3220.      storage for dynamically created variables accessed via other pointers
  3221.      in Modula-2). For example
  3222.  
  3223.           NEW( myBoss );
  3224.  
  3225.      NEW invokes the procedure ALLOCATEOBJECT, which allocates the
  3226.      necessary amount of storage to hold the object's data and then invokes
  3227.      the object's initialization code defined for the class.  This
  3228.      initialization code is defined by the statement sequence following
  3229.      INIT in the class declaration or class implementation. 
  3230.  
  3231.      Once you no longer need the object, you may get rid of it via the
  3232.      standard procedure DISPOSE. For example
  3233.  
  3234.           DISPOSE( myBoss );
  3235.  
  3236.      DISPOSE invokes the procedure DEALLOCATEOBJECT, which deallocates the
  3237.      necessary amount of storage to hold the object's data after invoking
  3238.      the object's destruction code defined for the class.  This destruction
  3239.      code is defined by the statement sequence following DESTROY in the
  3240.      class declaration or class implementation. 
  3241.  
  3242.      Implementations of the procedures ALLOCATEOBJECT and DEALLOCATEOBJECT
  3243.  
  3244.  
  3245.  
  3246.      Classes                                                             56
  3247.      
  3248.  
  3249.      are found in the library module Objects (or you can write your own). 
  3250.      Typically, an OOP module imports from Objects, of course. 
  3251.  
  3252.      It may be desirable to have multiple object handles reference the same
  3253.      object.  We can easily do this by assigning the value of an object
  3254.      handle to another object handle. 
  3255.  
  3256.           objHandle2 := objHandle1;
  3257.  
  3258.      Please note that we are assigning object handles, not making a copy of
  3259.      the object itself.  If we 'DISPOSE(objHandle1)', objHandle2 will hold
  3260.      an obsolete handle. 
  3261.  
  3262.      
  3263.  
  3264.  
  3265.  
  3266.  
  3267.      12.6 Object compatibility
  3268.  
  3269.  
  3270.      The possibility of assigning one object to another, or of passing
  3271.      objects as parameters to procedures or methods, brings us to the
  3272.      question of object type compatibility.  Now the fun really begins! 
  3273.  
  3274.      Two object handles are assignment compatible if they are of the same
  3275.      class type or if the class type of the source operand was derived from
  3276.      the class of the destination operand.  For example, considering the
  3277.      declarations above,
  3278.  
  3279.           VAR myBoss :Person;
  3280.               me     :Programmer (* inherits from Person *);
  3281.  
  3282.      me is assignment compatible with myBoss, but not the other way around:
  3283.  
  3284.               myBoss := me;       (* right! *)
  3285.               me := myBoss;       (* wrong *)
  3286.  
  3287.      If you think about it for a couple of minutes, you will agree that
  3288.      this makes sense.  The class Person has methods that know how to deal
  3289.      with objects of that class.  The class Programmer adds methods that
  3290.      know how to deal with the Programmer extensions to Person. If you
  3291.      applied a Programmer method (isSmart) to a non-Programmer Person, the
  3292.      method would fail because favoriteLanguage would not be a field of the
  3293.      Person structure.  But a Person method (isMale) would have no trouble
  3294.      handling a Programmer, whose structure includes all the Person
  3295.      information.  Let's not forget that the method Person.isMale is also a
  3296.      method of the class Programmer!
  3297.  
  3298.      The simple way to remember this is that an assignment is legal if the
  3299.      source can completely fill the destination (the fields that are left
  3300.      over are just "truncated", but not the reverse, as that would leave
  3301.      fields dangerously undefined). 
  3302.  
  3303.  
  3304.  
  3305.      Classes                                                             57
  3306.      
  3307.  
  3308.  
  3309.      Herein lies the power of object oriented programming.  As you derive
  3310.      more specialized versions of a class, you inherit all the work done by
  3311.      whoever implemented its superclass. 
  3312.  
  3313.      This implementation only supports "single inheritance" -- a subclass
  3314.      can only inherit directly from its one superclass.  If we had multiple
  3315.      inheritance it would be even more fun, as we could inherit even more
  3316.      of other people's work at once! 
  3317.  
  3318.      
  3319.  
  3320.  
  3321.      12.6.0.1 Type compatibility of VAR object parameters
  3322.  
  3323.      The compatibility rules that govern assignment between pointers also
  3324.      apply to VAR object parameters. 
  3325.  
  3326.      
  3327.  
  3328.  
  3329.      12.6.0.2 Compatibility between Objects and other types
  3330.  
  3331.      Object handles are compatible with NIL and SYSTEM.ADDRESS.
  3332.  
  3333.      
  3334.  
  3335.  
  3336.  
  3337.  
  3338.      12.7 The MEMBER function
  3339.  
  3340.  
  3341.      Clearly situations will arise where a procedure or method needs to
  3342.      know exactly what type its parameters are.  This implementation makes
  3343.      available a standard function MEMBER to do just this. 
  3344.  
  3345.           MEMBER( objectHandle, className )
  3346.  
  3347.      will return TRUE if the object is of the type className or className
  3348.      is an ancestor of the object's class. 
  3349.  
  3350.      To use the function MEMBER, you must import MEMBEROBJECT from Objects
  3351.      (or write your own version of MEMBEROBJECT).
  3352.  
  3353.      
  3354.  
  3355.  
  3356.  
  3357.  
  3358.      12.8 Complete example
  3359.  
  3360.  
  3361.  
  3362.  
  3363.  
  3364.      Classes                                                             58
  3365.      
  3366.  
  3367.      Here is a simple example to illustrate the ideas of the last sections:
  3368.      using the classes declared in People.
  3369.  
  3370.           MODULE UsePeople;
  3371.             FROM InOut IMPORT
  3372.               WriteString, WriteLn;
  3373.             FROM People IMPORT
  3374.               Person, Programmer;
  3375.             FROM Objects IMPORT
  3376.               ALLOCATEOBJECT,DEALLOCATEOBJECT,MEMBEROBJECT;
  3377.             VAR
  3378.               someone         :Person;
  3379.               smartProgrammer :Programmer;
  3380.             BEGIN
  3381.               NEW(smartProgrammer);
  3382.               smartProgrammer.favoriteLanguage := "Modula-2";
  3383.               someone := smartProgrammer;
  3384.               IF MEMBER(someone,Programmer) &
  3385.                  smartProgrammer.isSmart()
  3386.                  THEN
  3387.                   WriteString( "he is a smart programmer!" );
  3388.                   WriteLn;
  3389.               END;
  3390.           END UsePeople.
  3391.  
  3392.      
  3393.  
  3394.  
  3395.  
  3396.  
  3397.      12.9 Virtual methods
  3398.  
  3399.  
  3400.      A programmer coming up with code like that above would be justifiably
  3401.      proud of the definition of a smart programmer...  however, his boss,
  3402.      who might not know Modula-2, might not have liked it.  As he put it,
  3403.      how smart a programmer is has more to do with the tools that he choses
  3404.      and how he uses them than simply what his favorite language is! 
  3405.      Hmmm...
  3406.  
  3407.      To satisfy the boss, suppose we come up with this new class:
  3408.  
  3409.           DEFINITION MODULE MyKindOfProgrammers;
  3410.           FROM People IMPORT Programmer, LanguageName;
  3411.  
  3412.           CLASS Modula2Programmer;
  3413.             INHERIT Programmer;
  3414.             compilerVendor :ARRAY [0..2] OF CHAR;
  3415.           END Modula2Programmer;
  3416.  
  3417.           END MyKindOfProgrammers.
  3418.  
  3419.      This might be implemented as follows:
  3420.  
  3421.  
  3422.  
  3423.      Classes                                                             59
  3424.      
  3425.  
  3426.  
  3427.           IMPLEMENTATION MODULE MyKindOfProgrammers;
  3428.           FROM Strings IMPORT CompareStr;
  3429.           CLASS Modula2Programmer;
  3430.             PROCEDURE isSmart() :BOOLEAN;
  3431.               BEGIN
  3432.                 RETURN CompareStr(compilerVendor,"FST") = 0;
  3433.               END isSmart;
  3434.             INIT
  3435.               compilerVendor := "FST";
  3436.           END Modula2Programmer;
  3437.  
  3438.           END MyKindOfProgrammers.
  3439.  
  3440.      What about the boss?!  Well, he can create his own programmer classes
  3441.      if he wants to! 
  3442.  
  3443.      What have we done?  Let's see how these classes could be used:
  3444.  
  3445.           MODULE x;
  3446.           FROM InOut IMPORT WriteString, WriteLn;
  3447.           FROM People IMPORT Programmer;
  3448.           FROM MyKindOfProgrammers IMPORT Modula2Programmer;
  3449.           FROM Objects IMPORT
  3450.             ALLOCATEOBJECT,DEALLOCATEOBJECT,MEMBEROBJECT;
  3451.  
  3452.           CLASS CProgrammer;
  3453.             INHERIT Programmer;
  3454.           END CProgrammer;
  3455.  
  3456.           VAR m2programmer :Modula2Programmer;
  3457.               cprogrammer  :CProgrammer;
  3458.  
  3459.           BEGIN
  3460.             NEW( m2programmer );
  3461.             NEW( cprogrammer );
  3462.             IF m2programmer.isSmart() THEN
  3463.               WriteString( "this m2 programmer is smart" ); WriteLn;
  3464.             END;
  3465.             IF cprogrammer.isSmart() THEN
  3466.               WriteString( "this C programmer is smart" ); WriteLn;
  3467.             END;
  3468.           END x.
  3469.  
  3470.      This program would output "this m2 programmer is smart". 
  3471.  
  3472.      In the implementation of Modula2Programmer we have effectively
  3473.      redefined the method isSmart, and so the message m2programmer.isSmart
  3474.      invokes this new method.  But since CProgrammer did not redefine
  3475.      isSmart, the message cprogrammer.isSmart invokes the method
  3476.      implemented in Programmer. Makes sense, does it not? 
  3477.  
  3478.      Now let us consider the following (similar) example:
  3479.  
  3480.  
  3481.  
  3482.      Classes                                                             60
  3483.      
  3484.  
  3485.  
  3486.           MODULE y;
  3487.           FROM Objects IMPORT ALLOCATEOBJECT;
  3488.           FROM InOut IMPORT WriteString, WriteLn;
  3489.           FROM People IMPORT Programmer;
  3490.           FROM MyKindOfProgrammers IMPORT Modula2Programmer;
  3491.  
  3492.           CLASS CProgrammer;
  3493.             INHERIT Programmer;
  3494.           END CProgrammer;
  3495.  
  3496.           VAR m2programmer :Modula2Programmer;
  3497.               cprogrammer  :CProgrammer;
  3498.  
  3499.           PROCEDURE ifIsSmart( p :Programmer; s :ARRAY OF CHAR );
  3500.             BEGIN
  3501.               IF p.isSmart() THEN
  3502.                 WriteString( s ); WriteLn;
  3503.               END;
  3504.             END ifIsSmart;
  3505.  
  3506.           BEGIN
  3507.             NEW( m2programmer );
  3508.             NEW( cprogrammer );
  3509.             ifIsSmart( m2programmer, "1st m2 programmer is smart" );
  3510.             ifIsSmart( cprogrammer, "1st C programmer is smart" );
  3511.             m2programmer.favoriteLanguage := "C";
  3512.             cprogrammer.favoriteLanguage := "Modula-2";
  3513.             ifIsSmart( m2programmer, "2nd m2 programmer is smart" );
  3514.             ifIsSmart( cprogrammer, "2nd C programmer is smart" );
  3515.           END y.
  3516.  
  3517.      In this case we would get the following result:
  3518.  
  3519.           1st m2 programmer is smart
  3520.           2nd m2 programmer is smart
  3521.           2nd C programmer is smart
  3522.  
  3523.      This may not be quite as obvious, although it certainly is what we
  3524.      would have wanted: Modula2Programmer's are smart, regardless of their
  3525.      favorite language, if they use FST Modula-2. By default, programmers
  3526.      are smart if their favorite language is Modula-2.
  3527.  
  3528.      Note further that
  3529.  
  3530.           MEMBER( cprogrammer, Modula2Programmer )
  3531.  
  3532.      would have returned FALSE, but
  3533.  
  3534.           MEMBER( cprogrammer, Person )
  3535.  
  3536.      would have returned TRUE.
  3537.  
  3538.  
  3539.  
  3540.  
  3541.      Classes                                                             61
  3542.      
  3543.  
  3544.      How is it that the ifIsSmart method is invoking different procedures
  3545.      at different times, when the programmed call is, obviously, the same? 
  3546.  
  3547.      Enter the world of dynamic binding.  The procedure (method) invoked by
  3548.      p.isSmart in ifIsSmart is not determined until run time and it depends
  3549.      on the actual run time type of the object referenced by p. 
  3550.  
  3551.      
  3552.  
  3553.  
  3554.  
  3555.  
  3556.      12.10 For the technically minded
  3557.  
  3558.  
  3559.      Hmmm... Okay, time for a technical digression.  Skip all this if you
  3560.      wish! 
  3561.  
  3562.      When we declare a CLASS, the compiler generates a class template.  The
  3563.      class template is a structure (the header of which is defined in
  3564.      Objects.DEF and shown bellow) that contains a reference to the parent
  3565.      class, the size of objects of this class and a pointer to each method
  3566.      in this class. 
  3567.  
  3568.           ClassHeader = RECORD
  3569.               parent      :Class;
  3570.               size        :CARDINAL;
  3571.               filler      :CARDINAL;
  3572.               InitProc    :PROCEDURE( ADDRESS );
  3573.               DestroyProc :PROCEDURE( ADDRESS );
  3574.               ... other virtual methods ...
  3575.           END;
  3576.  
  3577.      When an object is created, the ALLOCATEOBJECT procedure allocates
  3578.      enough space to hold the data in the object's structure plus a pointer
  3579.      to the object's class.  So, as you can see, regardless of the declared
  3580.      type of the object handle that refers to a particular object, we can,
  3581.      by looking at the object itself, determine what its real type is. 
  3582.  
  3583.      As you might have guessed by now, we invoke a method by picking up its
  3584.      address from the class template referenced by the object itself. 
  3585.  
  3586.      There is only one exception to this.  As was stated earlier, methods
  3587.      declared in implementation modules are static.  These methods cannot
  3588.      be redefined and, therefore, are not placed in the class template. 
  3589.  
  3590.      In summary:
  3591.  
  3592.           A virtual method may be redefined and its invocation is
  3593.           dynamically resolved at run time. 
  3594.  
  3595.           A static method cannot be redefined and its invocation is
  3596.           resolved at compile time. 
  3597.  
  3598.  
  3599.  
  3600.      Classes                                                             62
  3601.      
  3602.  
  3603.  
  3604.           All methods defined in a definition module are virtual.  All
  3605.           methods first declared in a program or implementation module are
  3606.           static. 
  3607.  
  3608.      It may also help to give a more insightful explanation of the methods
  3609.      INIT and DESTROY.
  3610.  
  3611.      We have seen that the methods defined in INIT and DESTROY are
  3612.      automatically invoked when an object is created or destroyed. 
  3613.  
  3614.      When the compiler sees a NEW(objectHandle), it generates code that
  3615.      invokes the procedure ALLOCATEOBJECT, which must be known in the
  3616.      current environment -- you must either import this procedure from the
  3617.      standard module Objects or write your own. 
  3618.  
  3619.      ALLOCATEOBJECT takes two parameters: a VAR object handle and a class
  3620.      identifier (pointer to the class template). 
  3621.  
  3622.      First, ALLOCATEOBJECT calls Storage.ALLOCATE to allocate memory for
  3623.      the object and stores the address of the class template in the first
  3624.      double word of the object just allocated. 
  3625.  
  3626.      Next, and starting with the base class in the hierarchy,
  3627.      ALLOCATEOBJECT invokes all the INIT methods that were defined along
  3628.      the way.  In the previous example, the call NEW(m2programmer) caused
  3629.      person.INIT, Programmer.INIT and Modula2Programmer.INIT to be invoked,
  3630.      in that sequence. 
  3631.  
  3632.      For DISPOSE(objectHandle), the compiler generates a call to
  3633.      DEALLOCATEOBJECT, also defined in Objects.
  3634.  
  3635.      DEALLOCATEOBJECT works in the reverse order of ALLOCATEOBJECT. It
  3636.      invokes all the DESTROY methods defined in the class hierarchy,
  3637.      starting with the class of the current object.  It then invokes
  3638.      Storage.DEALLOCATE to free the object's memory. 
  3639.  
  3640.      
  3641.  
  3642.  
  3643.  
  3644.  
  3645.      12.11 On your own...
  3646.  
  3647.  
  3648.      This concludes our introduction to object oriented programming using
  3649.      FST Modula-2 v3.0. 
  3650.  
  3651.      For another small example of the use of classes, please look at the
  3652.      modules Lists, MyLists and Uselists.
  3653.  
  3654.      
  3655.  
  3656.  
  3657.  
  3658.  
  3659.      Shareware                                                           63
  3660.      
  3661.  
  3662.  
  3663.  
  3664.  
  3665.  
  3666.  
  3667.  
  3668.                                    Chapter 13
  3669.  
  3670.                                     Shareware
  3671.  
  3672.  
  3673.  
  3674.      This software package is distributed as Shareware.
  3675.  
  3676.      If you try this program and continue to use it, you are expected to
  3677.      register with us. 
  3678.  
  3679.      This software can be freely distributed, as long as no money is
  3680.      charged for it, all the files are included, unmodified, and with their
  3681.      modification dates preserved. 
  3682.  
  3683.      This software cannot be distributed as a part of another product. 
  3684.  
  3685.      This software cannot be used in a commercial environment without the
  3686.      payment of the proper license fee. 
  3687.  
  3688.      Our success will depend not only on the quality of this software but
  3689.      on the willingness of every individual user to "support" its
  3690.      developers.  If you use this product, please send in the registration
  3691.      form in the back of this document, along with your registration fee. 
  3692.      For a modest $50, we will send you the latest version of this software
  3693.      and all the library and runtime support source code.  This source code
  3694.      is made available to registered users only. 
  3695.  
  3696.      Whether or not you use this product, please give complete copies of
  3697.      this software to others. 
  3698.  
  3699.      
  3700.  
  3701.  
  3702.  
  3703.  
  3704.  
  3705.  
  3706.  
  3707.  
  3708.  
  3709.  
  3710.  
  3711.  
  3712.  
  3713.  
  3714.  
  3715.  
  3716.  
  3717.  
  3718.      License terms                                                       64
  3719.      
  3720.  
  3721.  
  3722.  
  3723.  
  3724.  
  3725.  
  3726.  
  3727.                                    Chapter 14
  3728.  
  3729.                                   License terms
  3730.  
  3731.  
  3732.  
  3733.      Before you register this product and become a licensed user, you are
  3734.      granted a limited license to evaluate the product to determine whether
  3735.      or not it will fit your needs.  Use of this system for any other
  3736.      purpose, before registration and without our written consent, is
  3737.      expressly forbidden. 
  3738.  
  3739.      Registered users are given a non-exclusive license to use this
  3740.      software on any machine that they have access to, but not on more than
  3741.      one at a time (the "treat this software like a book" idea). 
  3742.  
  3743.      Registered users may modify the source code provided to suit their
  3744.      needs, but this source code (in original or modified form) may not be
  3745.      distributed without the prior written consent of Fitted Software
  3746.      Tools.
  3747.  
  3748.      Registered users may include compiled portions of the library and
  3749.      runtime support code in the programs by them developed, and use or
  3750.      distribute these programs without payment of any additional license
  3751.      fees to Fitted Software Tools.
  3752.  
  3753.      We also provide an "educational site license".  Schools may opt for
  3754.      this $250 license, instead of licensing enough copies of the compiler
  3755.      for all the machines that the compiler will be installed on. 
  3756.  
  3757.      
  3758.  
  3759.  
  3760.  
  3761.  
  3762.  
  3763.  
  3764.  
  3765.  
  3766.  
  3767.  
  3768.  
  3769.  
  3770.  
  3771.  
  3772.  
  3773.  
  3774.  
  3775.  
  3776.  
  3777.      Support                                                             65
  3778.      
  3779.  
  3780.  
  3781.  
  3782.  
  3783.  
  3784.  
  3785.  
  3786.                                    Chapter 15
  3787.  
  3788.                                      Support
  3789.  
  3790.  
  3791.  
  3792.      Support for this product is available by mail or through our BBS at
  3793.      214/517-4629.
  3794.  
  3795.      
  3796.  
  3797.  
  3798.  
  3799.  
  3800.  
  3801.  
  3802.  
  3803.  
  3804.  
  3805.  
  3806.  
  3807.  
  3808.  
  3809.  
  3810.  
  3811.  
  3812.  
  3813.  
  3814.  
  3815.  
  3816.  
  3817.  
  3818.  
  3819.  
  3820.  
  3821.  
  3822.  
  3823.  
  3824.  
  3825.  
  3826.  
  3827.  
  3828.  
  3829.  
  3830.  
  3831.  
  3832.  
  3833.  
  3834.  
  3835.  
  3836.  
  3837.  
  3838.                                   COMMENT FORM
  3839.  
  3840.  
  3841.      How did you first learn about this product?
  3842.  
  3843.           ---------------------------------------------------------
  3844.  
  3845.      Where did you get this software from?
  3846.  
  3847.           ( ) us                   ( ) a bulletin board
  3848.           ( ) a friend                 phone # ___ ___ ____
  3849.           ( ) a computer club      ( ) a shareware software distributor
  3850.           ( ) other ________________________
  3851.  
  3852.      Systems you intend to use this software on
  3853.  
  3854.           ( ) PC (8088/8086)   ( ) AT (80286)   ( ) 80386   ( ) 80486
  3855.  
  3856.      Typical system's configuration
  3857.  
  3858.           ( ) hard disk
  3859.           ( ) 512k       ( ) 640k
  3860.           ( ) extended memory
  3861.           ( ) expanded memory (EMS, EEMS)
  3862.           ( ) EGA adapter
  3863.           ( ) VGA adapter
  3864.  
  3865.      What programming languages do you use regularly?
  3866.  
  3867.           ---------------------------------------------------------
  3868.  
  3869.      What do you like the most about this system?
  3870.  
  3871.           _____________________________________________________________
  3872.           _____________________________________________________________
  3873.           _____________________________________________________________
  3874.           _____________________________________________________________
  3875.           _____________________________________________________________
  3876.  
  3877.      What do you NOT like about this system?
  3878.  
  3879.           _____________________________________________________________
  3880.           _____________________________________________________________
  3881.           _____________________________________________________________
  3882.           _____________________________________________________________
  3883.           _____________________________________________________________
  3884.  
  3885.      Other comments / suggestions:
  3886.  
  3887.  
  3888.  
  3889.  
  3890.  
  3891.  
  3892.  
  3893.  
  3894.  
  3895.  
  3896.  
  3897.                                    BUG REPORT
  3898.  
  3899.      
  3900.  
  3901.  
  3902.      Name_____________________________________________
  3903.  
  3904.      Company__________________________________________
  3905.  
  3906.      Address__________________________________________
  3907.  
  3908.      City, State, Zip_________________________________
  3909.  
  3910.      Country__________________________________________
  3911.  
  3912.      Telephone _____________
  3913.  
  3914.  
  3915.      Version of the software in use:  ___________________________
  3916.  
  3917.      Machine in use (make and model): ___________________________
  3918.  
  3919.      Memory / Disks / Display type:   ___________________________
  3920.  
  3921.      Operating system used:           ___________________________
  3922.  
  3923.      Other system information that may be pertinent (PATH settings,
  3924.      CONFIG.SYS contents,...) 
  3925.  
  3926.  
  3927.  
  3928.  
  3929.  
  3930.      Problem description:
  3931.  
  3932.  
  3933.  
  3934.  
  3935.  
  3936.  
  3937.  
  3938.  
  3939.  
  3940.  
  3941.  
  3942.  
  3943.  
  3944.  
  3945.  
  3946.  
  3947.  
  3948.  
  3949.  
  3950.  
  3951.  
  3952.  
  3953.  
  3954.  
  3955.  
  3956.                                 REGISTRATION FORM
  3957.  
  3958.      Please mail this form to:
  3959.  
  3960.           Fitted Software Tools
  3961.           P.O.Box 867403
  3962.           Plano, TX 75086
  3963.  
  3964.      to register your copy/copies of the Modula-2 compiler, register/order
  3965.      additional copies, and/or order updates. 
  3966.  
  3967.      The unauthorized distribution of the library and runtime support
  3968.      source code included in the distribution disks that you receive when
  3969.      you order a registered copy or update is specifically prohibited. 
  3970.  
  3971.      If you are ordering multiple copies for your organization (or for you
  3972.      and your friends), we suggest that you order 1 of the $50 packages and
  3973.      pay $25 for each additional copy that you will make. 
  3974.  
  3975.  
  3976.      Name_____________________________________________
  3977.  
  3978.      Company__________________________________________
  3979.  
  3980.      Address__________________________________________
  3981.  
  3982.      City, State, Zip_________________________________
  3983.  
  3984.      Country__________________________________________
  3985.  
  3986.  
  3987.      QTY
  3988.      ___   X   Registration & latest version @ $50.00 (*)   _________
  3989.  
  3990.      ___   X   Additional Registration(s) @ $25.00          _________
  3991.  
  3992.      ___   X   Educational site license @ $250.00           _________
  3993.  
  3994.      Total enclosed (**):                                   _________
  3995.  
  3996.  
  3997.  
  3998.      Disk format preferred:  5-1/4"  [ ] 360k  [ ] 1.2MB
  3999.                              3.5"    [ ] 720k  [ ] 1.44MB
  4000.  
  4001.      Version of the software (the one displayed in the compiler screen)
  4002.      that you are currently using __________
  4003.  
  4004.      (*) We pay shipping worldwide.  (**) Cheque or money order payable in
  4005.      the USA, please! 
  4006.  
  4007.      
  4008.  
  4009.  
  4010.  
  4011.  
  4012.  
  4013.  
  4014.  
  4015.  
  4016.  
  4017.  
  4018.                            Table of Contents
  4019.  
  4020.  
  4021.      Chapter 1 Introduction                                           3
  4022.  
  4023.         1.1 A few words from the author                               3
  4024.         1.2 Hardware requirements                                     5
  4025.         1.3 Software requirements                                     5
  4026.         1.4 For users upgrading to release 3                          5
  4027.  
  4028.      Chapter 2 Software installation                                  6
  4029.  
  4030.         2.1 Theory                                                    6
  4031.         2.2 Recommended setup for a system with 2 floppy drives
  4032.                                                                       8
  4033.         2.3 Recommended setup for a hard disk system                  8
  4034.  
  4035.      Chapter 3 A little tour through the system                       10
  4036.  
  4037.         3.1 The tour                                                  10
  4038.  
  4039.      Chapter 4 The Compiler                                           13
  4040.  
  4041.         4.1 Running the integrated compiler: MC                       14
  4042.         4.2 Running the freestanding compiler: M2COMP                 16
  4043.         4.3 The compilation process                                   16
  4044.  
  4045.               4.3.0.1 The input file                                  16
  4046.               4.3.0.2 The imported modules                            16
  4047.               4.3.0.3 The output file                                 17
  4048.               4.3.0.4 A warning                                       17
  4049.  
  4050.         4.4 Compiler directives                                       17
  4051.  
  4052.               4.4.0.1 Benchmarks                                      18
  4053.  
  4054.         4.5 Runtime errors                                            18
  4055.  
  4056.            4.5.1 Trapping runtime errors in your program              19
  4057.  
  4058.         4.6 Compiler size limits                                      19
  4059.         4.7 The language supported                                    20
  4060.  
  4061.            4.7.1 LONGINT and LONGCARD                                 20
  4062.            4.7.2 LONGREAL                                             21
  4063.            4.7.3 Additional or augmented standard procedures          21
  4064.  
  4065.               4.7.3.1 NEW and DISPOSE -- pointer argument.            21
  4066.               4.7.3.2 LONG and SHORT                                  22
  4067.               4.7.3.3 FLOAT and TRUNC                                 22
  4068.  
  4069.  
  4070.  
  4071.  
  4072.  
  4073.         4.8 Objects exported by the pseudo module SYSTEM              22
  4074.  
  4075.               4.8.0.1 TYPE BYTE                                       22
  4076.               4.8.0.2 TYPE WORD                                       23
  4077.               4.8.0.3 TYPE ADDRESS                                    23
  4078.               4.8.0.4 SEG and OFS                                     23
  4079.               4.8.0.5 PROCEDURE ADR                                   24
  4080.               4.8.0.6 PROCEDURE FLAT                                  24
  4081.               4.8.0.7 PROCEDURE PTR                                   24
  4082.               4.8.0.8 PROCEDURE SEGMENT                               24
  4083.               4.8.0.9 PROCEDURE OFFSET                                24
  4084.               4.8.0.10 PROCEDURE NEWPROCESS                           24
  4085.               4.8.0.11 PROCEDURE TRANSFER                             24
  4086.               4.8.0.12 PROCEDURE IOTRANSFER                           25
  4087.               4.8.0.13 ASSEMBLER                                      25
  4088.               4.8.0.14 ASSEMBLER - 8087 support                       26
  4089.  
  4090.         4.9 The generated object code                                 27
  4091.  
  4092.            4.9.1 Data type representation                             27
  4093.            4.9.2 The runtime memory map                               28
  4094.            4.9.3 Procedure calling conventions                        29
  4095.  
  4096.               4.9.3.1 Parameter passing (all except open array
  4097.                     parameters)                                       29
  4098.               4.9.3.2 Parameter passing (open array parameters)
  4099.                                                                       29
  4100.               4.9.3.3 Returning values from a function procedure
  4101.                                                                       29
  4102.  
  4103.         4.10 Module priorities                                        30
  4104.         4.11 Memory models                                            31
  4105.  
  4106.      Chapter 5 Using OBJ files                                        32
  4107.  
  4108.         5.1 GenLink                                                   32
  4109.         5.2 Foreign Modules                                           32
  4110.  
  4111.            5.2.1 External names                                       33
  4112.            5.2.2 Implementation                                       33
  4113.  
  4114.               5.2.2.1 FOREIGN C modules                               33
  4115.               5.2.2.2 Parameter passing                               34
  4116.  
  4117.            5.2.3 In the real world...                                 35
  4118.  
  4119.      Chapter 6 The Text Editor                                        36
  4120.  
  4121.      Chapter 7 The Linker                                             38
  4122.  
  4123.         7.1 Module keys                                               39
  4124.  
  4125.      Chapter 8 Other utilities                                        40
  4126.  
  4127.         8.1 Editor configurator                                       40
  4128.  
  4129.  
  4130.  
  4131.  
  4132.         8.2 Map file generator                                        40
  4133.         8.3 Make and the Makefile generator                           41
  4134.         8.4 M2O file decoder                                          42
  4135.         8.5 The execution profiler                                    42
  4136.  
  4137.      Chapter 9 The Library Modules                                    43
  4138.  
  4139.         9.1 Release 3.0 libraries                                     43
  4140.  
  4141.      Chapter 10 The runtime support system                            44
  4142.  
  4143.      Chapter 11 Modula-2 Syntax                                       45
  4144.  
  4145.      Chapter 12 Classes                                               47
  4146.  
  4147.         12.1 Highlights of the implementation                         47
  4148.         12.2 Classes, attributes, methods, inheritance and
  4149.                     polymorphism                                      48
  4150.         12.3 Defining classes in definition modules                   51
  4151.         12.4 Implementing classes in implementation modules           53
  4152.         12.5 Object instantiation                                     55
  4153.         12.6 Object compatibility                                     56
  4154.  
  4155.               12.6.0.1 Type compatibility of VAR object
  4156.                     parameters                                        57
  4157.               12.6.0.2 Compatibility between Objects and other
  4158.                     types                                             57
  4159.  
  4160.         12.7 The MEMBER function                                      57
  4161.         12.8 Complete example                                         57
  4162.         12.9 Virtual methods                                          58
  4163.         12.10 For the technically minded                              61
  4164.         12.11 On your own...                                          62
  4165.  
  4166.      Chapter 13 Shareware                                             63
  4167.  
  4168.      Chapter 14 License terms                                         64
  4169.  
  4170.      Chapter 15 Support                                               65
  4171.  
  4172.  
  4173.  
  4174.  
  4175.  
  4176.  
  4177.  
  4178.  
  4179.  
  4180.  
  4181.  
  4182.  
  4183.  
  4184.  
  4185.  
  4186.  
  4187.  
  4188.  
  4189.  
  4190.